标签:des style http color io os ar strong for
摘要:而今微信已经深入到了每一个手机之中,是每款手机必备的APP,我们最常用的莫过于分享和登录。接下来我们来看看微信登录的相关说明。
微信登录授权开发
1、可以到微信开发平台注册相关的APP,等待审核成功后可获得对应的key和secret,获取成功后需要单独申请开通登录和支付接口,如图所示:
2、和QQ比较类似,需要填写Url Schemes,如demo中的wxd930ea5d5a258f4f ,然后引入相应framework。
3、在AppDelegate中注册和实现授权后的回调函数,代码如下:
01 |
//向微信注册 |
02 |
[WXApi registerApp:kWXAPP_ID withDescription:@ "weixin" ]; |
03 |
|
04 |
//授权后回调 WXApiDelegate |
05 |
-( void )onResp:(BaseReq *)resp |
06 |
{ |
07 |
/* |
08 |
ErrCode ERR_OK = 0(用户同意) |
09 |
ERR_AUTH_DENIED = -4(用户拒绝授权) |
10 |
ERR_USER_CANCEL = -2(用户取消) |
11 |
code 用户换取access_token的code,仅在ErrCode为0时有效 |
12 |
state 第三方程序发送时用来标识其请求的唯一性的标志,由第三方程序调用sendReq时传入,由微信终端回传,state字符串长度不能超过1K |
13 |
lang 微信客户端当前语言 |
14 |
country 微信用户当前国家信息 |
15 |
*/ |
16 |
SendAuthResp *aresp = (SendAuthResp *)resp; |
17 |
if (aresp.errCode== 0) { |
18 |
NSString *code = aresp.code; |
19 |
NSDictionary *dic = @{@ "code" :code}; |
20 |
} |
21 |
} |
22 |
|
23 |
//和QQ,新浪并列回调句柄 |
24 |
- ( BOOL )application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation |
25 |
{ |
26 |
return [TencentOAuth HandleOpenURL:url] || |
27 |
[WeiboSDK handleOpenURL:url delegate:self] || |
28 |
[WXApi handleOpenURL:url delegate:self];; |
29 |
} |
30 |
|
31 |
- ( BOOL )application:(UIApplication *)application handleOpenURL:(NSURL *)url |
32 |
{ |
33 |
return [TencentOAuth HandleOpenURL:url] || |
34 |
[WeiboSDK handleOpenURL:url delegate:self] || |
35 |
[WXApi handleOpenURL:url delegate:self];; |
36 |
} |
4、第一步:获取code,这个用来获取token
01 |
- (IBAction)weixinLogin:(id)sender |
02 |
{ |
03 |
[self sendAuthRequest]; |
04 |
} |
05 |
|
06 |
-( void )sendAuthRequest |
07 |
{ |
08 |
SendAuthReq* req =[[SendAuthReq alloc ] init]; |
09 |
req.scope = @ "snsapi_userinfo,snsapi_base" ; |
10 |
req.state = @ "0744" ; |
11 |
[WXApi sendReq:req]; |
12 |
} |
这里获取后会调用之前在AppDelegate里面对应的oauthResp回调,获得得到的code。
第二步:带上code获取token
01 |
-( void )getAccess_token |
02 |
{ |
04 |
|
05 |
NSString *url =[NSString stringWithFormat:@ "https://api.weixin.qq.com/sns/oauth2/access_token?appid=%@&secret=%@&code=%@&grant_type=authorization_code" ,kWXAPP_ID,kWXAPP_SECRET,self.wxCode.text]; |
06 |
|
07 |
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ |
08 |
NSURL *zoneUrl = [NSURL URLWithString:url]; |
09 |
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 |
/* |
15 |
{ |
16 |
"access_token" = "OezXcEiiBSKSxW0eoylIeJDUKD6z6dmr42JANLPjNN7Kaf3e4GZ2OncrCfiKnGWiusJMZwzQU8kXcnT1hNs_ykAFDfDEuNp6waj-bDdepEzooL_k1vb7EQzhP8plTbD0AgR8zCRi1It3eNS7yRyd5A"; |
17 |
"expires_in" = 7200; |
18 |
openid = oyAaTjsDx7pl4Q42O3sDzDtA7gZs; |
19 |
"refresh_token" = "OezXcEiiBSKSxW0eoylIeJDUKD6z6dmr42JANLPjNN7Kaf3e4GZ2OncrCfiKnGWi2ZzH_XfVVxZbmha9oSFnKAhFsS0iyARkXCa7zPu4MqVRdwyb8J16V8cWw7oNIff0l-5F-4-GJwD8MopmjHXKiA"; |
20 |
scope = "snsapi_userinfo,snsapi_base"; |
21 |
} |
22 |
*/ |
23 |
|
24 |
self.access_token.text = [dic objectForKey:@ "access_token" ]; |
25 |
self.openid.text = [dic objectForKey:@ "openid" ]; |
26 |
|
27 |
} |
28 |
}); |
29 |
}); |
30 |
} |
利用GCD来获取对应的token和openID。
第三步:根据第二步获取的token和openid获取用户的相关信息。
01 |
-( void )getUserInfo |
02 |
{ |
04 |
|
05 |
NSString *url =[NSString stringWithFormat:@ "https://api.weixin.qq.com/sns/userinfo?access_token=%@&openid=%@" ,self.access_token.text,self.openid.text]; |
06 |
|
07 |
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ |
08 |
NSURL *zoneUrl = [NSURL URLWithString:url]; |
09 |
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 |
/* |
15 |
{ |
16 |
city = Haidian; |
17 |
country = CN; |
18 |
headimgurl = "http://wx.qlogo.cn/mmopen/FrdAUicrPIibcpGzxuD0kjfnvc2klwzQ62a1brlWq1sjNfWREia6W8Cf8kNCbErowsSUcGSIltXTqrhQgPEibYakpl5EokGMibMPU/0"; |
19 |
language = "zh_CN"; |
20 |
nickname = "xxx"; |
21 |
openid = oyAaTjsDx7pl4xxxxxxx; |
22 |
privilege = ( |
23 |
); |
24 |
province = Beijing; |
25 |
sex = 1; |
26 |
unionid = oyAaTjsxxxxxxQ42O3xxxxxxs; |
27 |
} |
28 |
*/ |
29 |
|
30 |
self.nickname.text = [dic objectForKey:@ "nickname" ]; |
31 |
self.wxHeadImg.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[dic objectForKey:@ "headimgurl" ]]]]; |
32 |
|
33 |
} |
34 |
}); |
35 |
|
36 |
}); |
37 |
} |
执行到这一步就算完成了整个授权登录的功能,可以把你的昵称、头像显示出来,剩下的就是及时刷新你的token了。
》》》》》》点击查看全部
标签:des style http color io os ar strong for
原文地址:http://www.cnblogs.com/colin2014/p/4000156.html