iOS 微信第三方登录

项目中用到微信第三方登录,吐槽下微信登录demo,基本是没看。。。主要参照微信第三方登录文档,不得不说文档确实比demo好很多
1、在微信开放平台申请账号,注册应用,拿到appId和appSecret,后台部分搞定;
2、下载微信官方sdk,将需要的framework拖进工程中;
3、设置微信白名单和URL type 这里就忽略掉了;
4、集成开发
首先、在appdelegate里面注册appId(只要打开应用就会走的这个方法)

-(BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[WXApi registerApp:WX_App_ID withDescription:@""];
return YES;
} 

其次,在登录页面调起微信app

-(void)weChatLogin {
   if ([WXApi isWXAppInstalled]) { 
  SendAuthReq *req = [[SendAuthReq alloc] init];
 //这里是按照官方文档的说明来的此处我要获取的是个人信息内容 
  req.scope = @"snsapi_userinfo";
  req.state = @""; 
//向微信终端发起SendAuthReq消息 
  [WXApi sendReq:req];
} else { 
DPFLog(@"安装微信客户端");
}
} 

接下来,我们在微信里面为我们的app授权,此处微信里面操作; 授权完成之后回到我们app,微信的回应

// WXApiDelegate
 -(void)onResp:(BaseResp *)resp{
   if ([resp isKindOfClass:[SendAuthResp class]]) { 
         SendAuthResp *temp = (SendAuthResp *)resp;
   if (temp.code != NULL) {
//此处判断下返回来的code值是否为错误码 /*此处接口地址为微信官方提供,我们只需要将返回来的code值传入再配合appId和appSecret即可获取到accessToken,openId和refreshToken */ 
     NSString *accessUrlStr = [NSString stringWithFormat:@"%@/oauth2/access_token?appid=%@&secret=%@&code=%@&grant_type=authorization_code", WX_BASE_URL, WX_App_ID, WX_App_Secret, temp.code]; 

     [GameRequestTool getWithUrlString:accessUrlStr success:^(id data) { 
    NSDictionary *dict = [data objectFromJSONData]; 
    NSString *accessToken = [dict objectForKey:WX_ACCESS_TOKEN]; NSString *openID = [dict objectForKey:WX_OPEN_ID];
    NSString *refreshToken = [dict objectForKey:WX_REFRESH_TOKEN]; // 本地持久化,以便access_token的使用、刷新或者持续
    if (accessToken && ![accessToken isEqualToString:@""] && openID && ![openID isEqualToString:@""]) { 
       [[NSUserDefaults standardUserDefaults] setObject:accessToken forKey:WX_ACCESS_TOKEN];
       [[NSUserDefaults standardUserDefaults] setObject:openID forKey:WX_OPEN_ID]; 
       [[NSUserDefaults standardUserDefaults] setObject:refreshToken forKey:WX_REFRESH_TOKEN]; 
       [[NSUserDefaults standardUserDefaults] synchronize]; 
// 命令直接同步到文件里,来避免数据的丢失 
}
 //请求个人信息 
[self requestUserInfoDescription]; 
} failure:^(NSError *error) { 
       DPFLog(@"error -- %@", error); 
} showView:self.window];
 } else {
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"获取微信授权失败" preferredStyle:(UIAlertControllerStyleAlert)];
        [alertController addAction:[UIAlertAction actionWithTitle:@"确认" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {  
}]];
        [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alertController animated:YES completion:nil];
 }
}
} 

最后我们获取授权用户的详细信息

 -(void)requestUserInfoDescription {
//获取当上一步保存的accessToken 和 openId 
NSString *accessToken = [[NSUserDefaults standardUserDefaults] objectForKey:WX_ACCESS_TOKEN];
NSString *openID = [[NSUserDefaults standardUserDefaults] objectForKey:WX_OPEN_ID];
//使用微信官方提供的接口获取授权用户个人信息
NSString *userUrlStr = [NSString stringWithFormat:@"%@/userinfo?access_token=%@&openid=%@", WX_BASE_URL, accessToken, openID];
[GameRequestTool getWithUrlString:userUrlStr success:^(id data) { NSDictionary *dict = [data objectFromJSONData];
 DPFLog(@"dict -- %@", dict); 
NSString *nickname = [dict objectForKey:@"nickname"]; 
if (nickname != NULL) { 
   LogInViewController *loginVC = [[LogInViewController alloc]init]; 
   [self.window addSubview:loginVC.view]; 
} else { 
   DPFLog(@"登录失败、、、"); 
}
} failure:^(NSError *error) { 
  DPFLog(@"error -- %@", error);} showView:self.window];
}

以上就是iOS微信第三方登录,并且获取授权用户个人信息的过程,其实这里有个问题,我们需要将请求到的accessToken值放在服务器而不是放在客户端,然而,时间紧迫,最后也只能先放在客户端。。。

你可能感兴趣的:(iOS 微信第三方登录)