最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

iOS微信第三方登錄實(shí)例

 更新時(shí)間:2016年12月07日 10:30:20   作者:st646889325  
這篇文章主要為大家詳細(xì)介紹了iOS微信第三方登錄實(shí)現(xiàn)過程,一步一步告訴大家iOS微信實(shí)現(xiàn)第三方登錄的方法,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了iOS微信第三方登錄,供大家參考,具體內(nèi)容如下

一、準(zhǔn)備工作
1、到微信開放平臺(tái)注冊成開發(fā)者,獲取appid
2、導(dǎo)入WeChatConnection.framework
3、配置URL Schemes  輸入appid  例如wx29ce0f21ea982cb8

二、配置AppDelegate.m

1、 注冊微信

//微信登陸 
[WXApi registerApp:WeiXin_AppId withDescription:@"weixin"]; 

2、設(shè)置函數(shù)

//把代理設(shè)置到登陸視圖中
- (BOOL)application:(UIApplication *)application 
   handleOpenURL:(NSURL *)url 
{ 
  return [WXApi handleOpenURL:url delegate:[LoginViewController shareLogin]]; 
} 
- (BOOL)application:(UIApplication *)application 
      openURL:(NSURL *)url 
 sourceApplication:(NSString *)sourceApplication 
     annotation:(id)annotation 
{ 
  return [WXApi handleOpenURL:url delegate:[LoginViewController shareLogin]]; 
} 

三、登陸頁代碼

1、微信登錄授權(quán)比較復(fù)雜,相比QQ,新浪多了幾步,簡單說就是需要三步,第一步,獲取code,這個(gè)用來獲取token,第二步,就是帶上code獲取token,第三步,根據(jù)第二步獲取的token和openid來獲取用戶的相關(guān)信息

2、

第一步:獲取code

-(void)weiXinLogin 
{ 
  SendAuthReq* req =[[SendAuthReq alloc] init]; 
  req.scope = @"snsapi_userinfo,snsapi_base"; 
  req.state = @"0744" ; 
  [WXApi sendReq:req]; 
} 
 
-(void)onReq:(BaseReq *)req 
{ 
  NSLog(@"呵呵"); 
  [self msgHint:@"登陸失敗"]; 
} 
 
-(void)onResp:(BaseResp *)resp 
{ 
  SendAuthResp* sender = (SendAuthResp*)resp; 
  NSString* code = sender.code; 
  NSLog(@"啦啦 code = %@",code); 
   
  MBProgressHUD * hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES]; 
  hud.labelText = @"收取用戶信息.."; 
  [self getAccess_tokenWithCode:code]; 
} 

第二步 獲取token

-(void)getAccess_tokenWithCode:(NSString*)myCode 
{ 
  //https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code 
   
  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]; 
   
  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) { 
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]; 
        NSString* token = [dic objectForKey:@"access_token"]; 
        NSString* openid = [dic objectForKey:@"openid"]; 
        [self getUserInfoWithToken:token openId:openid]; 
        NSLog(@"token = %@",token); 
        NSLog(@"openid = %@",openid); 
         
         
      } 
    }); 
  }); 
} 

第三步:獲取用戶信息

-(void)getUserInfoWithToken:(NSString*)myToken openId:(NSString*)myOpenId 
{ 
  // https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID 
   
  NSString *url =[NSString stringWithFormat:@"https://api.weixin.qq.com/sns/userinfo?access_token=%@&openid=%@",myToken,myOpenId]; 
  NSLog(@"infoUrl = %@",url); 
  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) { 
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]; 
        NSString* nickName = [dic objectForKey:@"nickname"]; 
        NSString* wxHeadImgUrl = [dic objectForKey:@"headimgurl"]; 
         
        NSLog(@"nickName = %@",nickName); 
        NSLog(@"headImg = %@",wxHeadImgUrl); 
         
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; 
        [userDefaults setObject:ON forKey:LogState]; 
        [userDefaults setObject:ThirdFoudationLogin forKey:LogType]; 
        [userDefaults setObject:nickName forKey:LoginName]; 
        [userDefaults setObject:wxHeadImgUrl forKey:UserHeaderPath]; 
        [userDefaults synchronize]; 
         
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES]; 
        [self msgHint:@"微信登陸成功"]; 
        [self popView]; 
      } 
    }); 
     
  }); 
} 

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

墨脱县| 青川县| 张家川| 景泰县| 富平县| 南昌县| 崇礼县| 射阳县| 嘉黎县| 武清区| 长泰县| 峡江县| 于都县| 茶陵县| 两当县| 监利县| 离岛区| 汕尾市| 昌吉市| 德庆县| 巴林右旗| 綦江县| 尤溪县| 高碑店市| 孟村| 峡江县| 昭苏县| 清丰县| 呼和浩特市| 获嘉县| 洛川县| 洮南市| 名山县| 育儿| 广宁县| 且末县| 阿城市| 马山县| 上蔡县| 宁城县| 桂东县|