首先上微信开发者平台下载SDK,网址:
plist文件中添加LSApplicationQueriesSchemes数组 ,添加两个字符串 ,用于调起微信
导入依赖库SystemConfiguration.framework, libz.dylib, libsqlite3.0.dylib, libc++.dylib, Security.framework, CoreTelephony.framework, CFNetwork.framework
添加白名单
在你的工程文件中选择Build Setting,在"Other Linker Flags"中加入"-ObjC 和 -all_load"
在Appdelegate.h中遵守协议和添加头文件
#import "AppDelegate.h"
#import "WXApi.h"
@interface AppDelegate ()<WXApiDelegate>
@end
在Appdelegate.m中的didFinishLaunchingWithOptions方法中注册微信WX_AppId是你的微信开发者平台上的appId
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// 微信注册
[WXApi registerApp:WX_AppId];
return YES;
}
在Appdelegate.m中添加代理方法
// 这个方法是用于从微信返回第三方App
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString*,id> *)options NS_AVAILABLE_IOS(9_0){
[WXApi handleOpenURL:url delegate:self];
return YES;
}
Appdelegate.m中的回调方法
[self.window showMBHUDAlertWithMessage:@"微信授权失败" hide:1.0]; 它是提示框
- (void)onResp:(BaseResp *)resp{
// =============== 获得的微信登录授权回调 ============
if ([resp isMemberOfClass:[SendAuthResp class]]) {
NSLog(@"******************获得的微信登录授权******************");
SendAuthResp *aresp = (SendAuthResp *)resp;
if (aresp.errCode != 0 ) {
dispatch_async(dispatch_get_main_queue(), ^{
[self.window showMBHUDAlertWithMessage:@"微信授权失败" hide:1.0];
});
return;
}
NSString *code = aresp.code;
[self getWeiXinOpenId:code];
}
}
AppDelegate.m中用于获取token和openid
//通过code获取access_token,openid,unionid
- (void)getWeiXinOpenId:(NSString *)code{
NSString *url =[NSString stringWithFormat:@"https://api.weixin.qq.com/sns/oauth2/access_token?appid=%@&secret=%@&code=%@&grant_type=authorization_code",WX_AppId,WX_AppSerect,code];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSURL *zoneUrl = [NSURL URLWithString:url];
NSString *zoneStr = [NSString stringWithContentsOfURL:zoneUrl encoding:NSUTF8StringEncoding error:nil];
NSData *data1 = [zoneStr dataUsingEncoding:NSUTF8StringEncoding];
if (!data1) {
[self.window showMBHUDAlertWithMessage:@"微信授权失败" hide:1.0];
return ;
}
// 授权成功,获取token、openID字典
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data1 options:NSJSONReadingMutableContainers error:nil];
NSLog(@"token、openID字典===%@",dic);
self.access_token = dic[@"access_token"];
self.openid= dic[@"openid"];
// 获取微信用户信息
[self getUserInfo];
});
}
APPDelegate.m用户获取微信用户信息
-(void)getUserInfo
{
NSString *url =[NSString stringWithFormat:@"https://api.weixin.qq.com/sns/userinfo?access_token=%@&openid=%@",self.access_token,self.openid];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSURL *zoneUrl = [NSURL URLWithString:url];
NSString *zoneStr = [NSString stringWithContentsOfURL:zoneUrl encoding:NSUTF8StringEncoding error:nil];
NSData *data = [zoneStr dataUsingEncoding:NSUTF8StringEncoding];
dispatch_async(dispatch_get_main_queue(), ^{
// 获取用户信息失败
if (!data) {
[self.window showMBHUDAlertWithMessage:@"微信授权失败" hide:1.0];
return ;
}
// 获取用户信息字典
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSLog(@"用户信息字典:===%@",dic);
});
});
}
在点击微信登录事件中写入
if ([WXApi isWXAppInstalled]) {
//构造SendAuthReq结构体
SendAuthReq *req = [[SendAuthReq alloc] init];
req.scope = @"snsapi_userinfo";
req.state = @"App";
//第三方向微信终端发送一个SendAuthReq消息结构
[WXApi sendReq:req];
}else{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"请先安装微信客户端" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
[alert addAction:action];
[self presentViewController:alert animated:YES completion:nil];
}
完!!!