iOS微信第三方登录实例
程序员文章站
2024-02-17 08:39:40
本文实例为大家分享了ios微信第三方登录,供大家参考,具体内容如下
一、准备工作
1、到微信开放平台注册成开发者,获取appid
2、导入wechatconnecti...
本文实例为大家分享了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]; } }); }); }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。