iOS微信第三方登入執行個體_IOS

來源:互聯網
上載者:User

本文執行個體為大家分享了iOS微信第三方登入,供大家參考,具體內容如下

一、準備工作
1、到微信開放平台註冊成開發人員,擷取appid
2、匯入WeChatConnection.framework
3、配置URL Schemes  輸入appid  例如wx29ce0f21ea982cb8

二、配置AppDelegate.m

1、 註冊微信

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

2、設定函數

//把代理設定到登陸視圖中- (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、微信登入授權比較複雜,相比QQ,新浪多了幾步,簡單說就是需要三步,第一步,擷取code,這個用來擷取token,第二步,就是帶上code擷取token,第三步,根據第二步擷取的token和openid來擷取使用者的相關資訊

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];       }     });        }); } 

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援雲棲社區。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.