iOS 第三方登陆 —— 微信

来源:互联网 发布:查电话软件 编辑:程序博客网 时间:2024/05/17 07:23

一、准备工作

1、到微信开放平台注册成开发者,获取appid

2、导入WeChatConnection.framework

3、配置URL Schemes  输入appid  例如wx29ce0f21ea982cb8

二、配置AppDelegate.m

1、 注册微信

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. //微信登陆  
  2. [WXApi registerApp:WeiXin_AppId withDescription:@"weixin"];  

2、设置函数

//把代理设置到登陆视图中

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. - (BOOL)application:(UIApplication *)application  
  2.       handleOpenURL:(NSURL *)url  
  3. {  
  4.     return [WXApi handleOpenURL:url delegate:[LoginViewController shareLogin]];  
  5. }  

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. - (BOOL)application:(UIApplication *)application  
  2.             openURL:(NSURL *)url  
  3.   sourceApplication:(NSString *)sourceApplication  
  4.          annotation:(id)annotation  
  5. {  
  6.     return [WXApi handleOpenURL:url delegate:[LoginViewController shareLogin]];  
  7. }  


三、登陆页代码

1、微信登录授权比较复杂,相比QQ,新浪多了几步,简单说就是需要三步,第一步,获取code,这个用来获取token,第二步,就是带上code获取token,第三步,根据第二步获取的token和openid来获取用户的相关信息

2、第一步:获取code

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. -(void)weiXinLogin  
  2. {  
  3.     SendAuthReq* req =[[SendAuthReq alloc] init];  
  4.     req.scope = @"snsapi_userinfo,snsapi_base";  
  5.     req.state = @"0744" ;  
  6.     [WXApi sendReq:req];  
  7. }  
  8.   
  9. -(void)onReq:(BaseReq *)req  
  10. {  
  11.     NSLog(@"呵呵");  
  12.     [self msgHint:@"登陆失败"];  
  13. }  
  14.   
  15. -(void)onResp:(BaseResp *)resp  
  16. {  
  17.     SendAuthResp* sender = (SendAuthResp*)resp;  
  18.     NSString* code = sender.code;  
  19.     NSLog(@"啦啦 code = %@",code);  
  20.       
  21.     MBProgressHUD * hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];  
  22.     hud.labelText = @"收取用户信息..";  
  23.     [self getAccess_tokenWithCode:code];  
  24. }  


第二步 获取token

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. -(void)getAccess_tokenWithCode:(NSString*)myCode  
  2. {  
  3.     //https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code  
  4.       
  5.     NSString *url =[NSString stringWithFormat:@"https://api.weixin.qq.com/sns/oauth2/access_token?appid=%@&secret=%@&code=%@&grant_type=authorization_code",kWXAPP_ID,kWXAPP_SECRET,myCode];  
  6.       
  7.     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{  
  8.         NSURL *zoneUrl = [NSURL URLWithString:url];  
  9.         NSString *zoneStr = [NSString stringWithContentsOfURL:zoneUrl encoding:NSUTF8StringEncoding error:nil];  
  10.         NSData *data = [zoneStr dataUsingEncoding:NSUTF8StringEncoding];  
  11.         dispatch_async(dispatch_get_main_queue(), ^{  
  12.             if (data) {  
  13.                 NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];  
  14.                 NSString* token = [dic objectForKey:@"access_token"];  
  15.                 NSString* openid = [dic objectForKey:@"openid"];  
  16.                 [self getUserInfoWithToken:token openId:openid];  
  17.                 NSLog(@"token = %@",token);  
  18.                 NSLog(@"openid = %@",openid);  
  19.                   
  20.                   
  21.             }  
  22.         });  
  23.     });  
  24. }  


第三步:获取用户信息

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. -(void)getUserInfoWithToken:(NSString*)myToken openId:(NSString*)myOpenId  
  2. {  
  3.     // https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID  
  4.       
  5.     NSString *url =[NSString stringWithFormat:@"https://api.weixin.qq.com/sns/userinfo?access_token=%@&openid=%@",myToken,myOpenId];  
  6.     NSLog(@"infoUrl = %@",url);  
  7.     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{  
  8.         NSURL *zoneUrl = [NSURL URLWithString:url];  
  9.         NSString *zoneStr = [NSString stringWithContentsOfURL:zoneUrl encoding:NSUTF8StringEncoding error:nil];  
  10.         NSData *data = [zoneStr dataUsingEncoding:NSUTF8StringEncoding];  
  11.         dispatch_async(dispatch_get_main_queue(), ^{  
  12.             if (data) {  
  13.                 NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];  
  14.                 NSString* nickName = [dic objectForKey:@"nickname"];  
  15.                 NSString* wxHeadImgUrl = [dic objectForKey:@"headimgurl"];  
  16.                   
  17.                 NSLog(@"nickName = %@",nickName);  
  18.                 NSLog(@"headImg = %@",wxHeadImgUrl);  
  19.                   
  20.                 NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];  
  21.                 [userDefaults setObject:ON forKey:LogState];  
  22.                 [userDefaults setObject:ThirdFoudationLogin forKey:LogType];  
  23.                 [userDefaults setObject:nickName forKey:LoginName];  
  24.                 [userDefaults setObject:wxHeadImgUrl forKey:UserHeaderPath];  
  25.                 [userDefaults synchronize];  
  26.                   
  27.                 [MBProgressHUD hideAllHUDsForView:self.view animated:YES];  
  28.                 [self msgHint:@"微信登陆成功"];  
  29.                 [self popView];  
  30.             }  
  31.         });  
  32.           
  33.     });  
  34. }  
0 0
原创粉丝点击