项目地址 : https://github.com/zhonggaorong/weixinlogindemo
1.在进行微信oauth2.0授权登录接入之前,在微信开放平台注册开发者帐号,并拥有一个已审核通过的移动应用,并获得相应的appid和appsecret,申请微信登录且通过审核后,可开始接入流程。 地址: 点击打开链接
2. 下载最新的sdk 地址: 点击打开链接
sdk内容如下:
结构解析:
从上到下依次说明:
1. 静态库,直接拖入工程。
2. ready.text自己看
3. 授权sdk。
4. 登录方法所在类。
5. 一些常用的对象类。
ios微信登录注意事项:
1、目前移动应用上微信登录只提供原生的登录方式,需要用户安装微信客户端才能配合使用。
2、对于android应用,建议总是显示微信登录按钮,当用户手机没有安装微信客户端时,请引导用户下载安装微信客户端。
3、对于ios应用,考虑到ios应用商店审核指南中的相关规定,建议开发者接入微信登录时,先检测用户手机是否已安装微信客户端(使用sdk中iswxappinstalled函数 ),对未安装的用户隐藏微信登录按钮,只提供其他登录方式(比如手机号注册登录、游客登录等)。
ios微信登录大致流程:
1. 第三方发起微信授权登录请求,微信用户允许授权第三方应用后,微信会拉起应用或重定向到第三方网站,并且带上授权临时票据code参数;
2. 通过code参数加上appid和appsecret等,通过api换取access_token;
3. 通过access_token进行接口调用,获取用户基本数据资源或帮助用户实现基本操作。
示意图:
接下来就进入正题:
1.配置工程
1. 新建一个工程。
2. 把下载下来的sdk中的.h文件与静态库全部拖入工程。
3. 加入依赖库
4. url - types (加入 appid)
target - info - url types
5. 白名单
当程序出现此错误
-canopenurl: failed for url: "weixin://app/wx5efead4057f98bc0/" - error: "this app is not allowed to query for scheme weixin"
就说明没有针对ios9 增加白名单。在info.plist文件中加入 lsapplicationqueriesschemes
app transport security 这个是让程序还是用http进行请求。
lsapplicationqueriesschemes 这个是增加微信的白名单。
6. 现在编译应该是没有问题了。
2. 终于到令人兴奋的代码部分了。 直接上代码。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
// // appdelegate.m // weixinlogindemo // // created by 张国荣 on 16/6/20. // copyright © 2016年 bateorganization. all rights reserved. // #import "appdelegate.h" #import "wxapi.h" //微信开发者id #define url_appid @"app id" @end @implementation appdelegate - ( bool )application:(uiapplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions { //向微信注册应用。 [wxapi registerapp:url_appid withdescription:@ "wechat" ]; return yes; } -( bool )application:(uiapplication *)app openurl:(nsurl *)url options:(nsdictionary<nsstring *,id> *)options{ /*! @brief 处理微信通过url启动app时传递的数据 * * 需要在 application:openurl:sourceapplication:annotation:或者application:handleopenurl中调用。 * @param url 微信启动第三方应用时传递过来的url * @param delegate wxapidelegate对象,用来接收微信触发的消息。 * @return 成功返回yes,失败返回no。 */ return [wxapi handleopenurl:url delegate:self]; } /*! 微信回调,不管是登录还是分享成功与否,都是走这个方法 @brief 发送一个sendreq后,收到微信的回应 * * 收到一个来自微信的处理结果。调用一次sendreq后会收到onresp。 * 可能收到的处理结果有sendmessagetowxresp、sendauthresp等。 * @param resp具体的回应内容,是自动释放的 */ -( void ) onresp:(baseresp*)resp{ nslog(@ "resp %d" ,resp.errcode); /* enum wxerrcode { wxsuccess = 0, 成功 wxerrcodecommon = -1, 普通错误类型 wxerrcodeusercancel = -2, 用户点击取消并返回 wxerrcodesentfail = -3, 发送失败 wxerrcodeauthdeny = -4, 授权失败 wxerrcodeunsupport = -5, 微信不支持 }; */ if ([resp iskindofclass:[sendauthresp class ]]) { //授权登录的类。 if (resp.errcode == 0) { //成功。 //这里处理回调的方法 。 通过代理吧对应的登录消息传送过去。 if ([_wxdelegate respondstoselector:@selector(loginsuccessbycode:)]) { sendauthresp *resp2 = (sendauthresp *)resp; [_wxdelegate loginsuccessbycode:resp2.code]; } } else { //失败 nslog(@ "error %@" ,resp.errstr); uialertview *alert = [[uialertview alloc]initwithtitle:@ "登录失败" message:[nsstring stringwithformat:@ "reason : %@" ,resp.errstr] delegate:self cancelbuttontitle:@ "取消" otherbuttontitles:@ "确定" , nil nil]; [alert show]; } } } @end |
下面是登录的类。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
// // viewcontroller.m // weixinlogindemo // // created by 张国荣 on 16/6/20. // copyright © 2016年 bateorganization. all rights reserved. // #import "viewcontroller.h" #import "wxapi.h" #import "appdelegate.h" //微信开发者id #define url_appid @"appid" #define url_secret @"app secret" #import "afnetworking.h" @interface viewcontroller ()<wxdelegate> { appdelegate *appdelegate; } @end @implementation viewcontroller - ( void )viewdidload { [super viewdidload]; // do any additional setup after loading the view, typically from a nib. } #pragma mark 微信登录 - (ibaction)weixinloginaction:(id)sender { if ([wxapi iswxappinstalled]) { sendauthreq *req = [[sendauthreq alloc]init]; req.scope = @ "snsapi_userinfo" ; req.openid = url_appid; req.state = @ "1245" ; appdelegate = [uiapplication sharedapplication].delegate; appdelegate.wxdelegate = self; [wxapi sendreq:req]; } else { //把微信登录的按钮隐藏掉。 } } #pragma mark 微信登录回调。 -( void )loginsuccessbycode:(nsstring *)code{ nslog(@ "code %@" ,code); __weak typeof(*&self) weakself = self; afhttpsessionmanager *manager = [afhttpsessionmanager manager]; manager.requestserializer = [afjsonrequestserializer serializer]; //请求 manager.responseserializer = [afhttpresponseserializer serializer]; //响应 manager.responseserializer.acceptablecontenttypes = [nsset setwithobjects:@ "text/html" ,@ "application/json" , @ "text/json" ,@ "text/plain" , nil nil]; //通过 appid secret 认证code . 来发送获取 access_token的请求 [manager get:[nsstring stringwithformat:@ "https://api.weixin.qq.com/sns/oauth2/access_token?appid=%@&secret=%@&code=%@&grant_type=authorization_code" ,url_appid,url_secret,code] parameters:nil progress:^(nsprogress * _nonnull downloadprogress) { } success:^(nsurlsessiondatatask * _nonnull task, id _nullable responseobject) { //获得access_token,然后根据access_token获取用户信息请求。 nsdictionary *dic = [nsjsonserialization jsonobjectwithdata:responseobject options:nsjsonreadingmutablecontainers error:nil]; nslog(@ "dic %@" ,dic); /* access_token 接口调用凭证 expires_in access_token接口调用凭证超时时间,单位(秒) refresh_token 用户刷新access_token openid 授权用户唯一标识 scope 用户授权的作用域,使用逗号(,)分隔 unionid 当且仅当该移动应用已获得该用户的userinfo授权时,才会出现该字段 */ nsstring* accesstoken=[dic valueforkey:@ "access_token" ]; nsstring* openid=[dic valueforkey:@ "openid" ]; [weakself requestuserinfobytoken:accesstoken andopenid:openid]; } failure:^(nsurlsessiondatatask * _nullable task, nserror * _nonnull error) { nslog(@ "error %@" ,error.localizedfailurereason); }]; } -( void )requestuserinfobytoken:(nsstring *)token andopenid:(nsstring *)openid{ afhttpsessionmanager *manager = [afhttpsessionmanager manager]; manager.requestserializer = [afjsonrequestserializer serializer]; manager.responseserializer = [afhttpresponseserializer serializer]; [manager get:[nsstring stringwithformat:@ "https://api.weixin.qq.com/sns/userinfo?access_token=%@&openid=%@" ,token,openid] parameters:nil progress:^(nsprogress * _nonnull downloadprogress) { } success:^(nsurlsessiondatatask * _nonnull task, id _nullable responseobject) { nsdictionary *dic = (nsdictionary *)[nsjsonserialization jsonobjectwithdata:responseobject options:nsjsonreadingmutablecontainers error:nil]; nslog(@ "dic ==== %@" ,dic); } failure:^(nsurlsessiondatatask * _nullable task, nserror * _nonnull error) { nslog(@ "error %ld" ,( long )error.code); }]; } - ( void )didreceivememorywarning { [super didreceivememorywarning]; // dispose of any resources that can be recreated. } @end |
以上所述是小编给大家介绍的ios实现第三方微信登录方式实例解析(最新最全),希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!
原文链接:http://blog.csdn.net/zhonggaorong/article/details/51719050