微信三方登录(微信原生三方登录)

一、微信三方登录的准备工作

1.下载SDK
https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419319164&token=&lang=zh_CN
2.添加依赖

微信三方登录(微信原生三方登录)_第1张图片
20160620143744227.jpeg

3.选择项目,targets,点击info,点开下面的URL Types,点击加号
在URL Schemes里填写tencent + appid。
4..在info.plist文件中加入 LSApplicationQueriesSchemes 里添加
weixin和wechat

二、微信三方登录的代码实现。

1.微信SDK的初始化

#import "WXApi.h"
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [WXApi registerApp:@"appid"];//此为申请下来的key一般以wx开头
    return YES;
}

2.微信登录按钮点击后的代码

      #import "WXApi.h"

       SendAuthReq* req = [[SendAuthReq alloc ] init ];
        req.scope = @"snsapi_userinfo" ;
        req.state = @"123" ;

3.返回自己APP后执行的代码
3.1设置代理

// 从微信端打开第三方APP会调用此方法,此方法再调用代理的onResp方法
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
      [WXApi handleOpenURL:url delegate:self];
      
      return YES;
}

- (BOOL)application:(UIApplication*)application openURL:(NSURL *)url sourceApplication:(NSString*)sourceApplication annotation:(id)annotation{
    
    [WXApi handleOpenURL:url delegate:self];
   
    return YES;
    
}

3.2准守协议

WXApiDelegate

3.3回调的方法
通过code获取accessToken
通过accessToken获取用户数据

- (void)onResp:(BaseResp *)resp {

//    UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:@"信息" message:@"分享成功" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
//    [alertview show];
    if ([resp isKindOfClass:[SendAuthResp class]]) {   //授权登录的类。
        if (resp.errCode == 0) {  //成功。
            
            SendAuthResp *resp2 = (SendAuthResp *)resp;
            NSLog(@"微信code=%@",resp2.code);
            
            NSMutableDictionary *param = [NSMutableDictionary dictionary];
            param[@"appid"] = @"wxcab5e47b1e1fdd39";
            param[@"secret"] = @"decc38d7914cbf155f4cd63952509383";
            param[@"code"] = resp2.code;
            param[@"grant_type"] = @"authorization_code";

            [[AFHTTPSessionManager manager] GET:@"https://api.weixin.qq.com/sns/oauth2/access_token" parameters:param progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//                NSLog(@"-----%@------",responseObject[@"access_token"]);
                
                NSMutableDictionary *param2 = [NSMutableDictionary dictionary];
                param2[@"access_token"] = responseObject[@"access_token"];
                param2[@"openid"] = @"wxcab5e47b1e1fdd39";
                
                [[AFHTTPSessionManager manager] GET:@"https://api.weixin.qq.com/sns/userinfo" parameters:param2 progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                    NSLog(@"-----%@------",responseObject);
                } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                    NSLog(@"--------%@",error);
                }];
            
            } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                NSLog(@"--------%@",error);
            }];
            }
        }else{ //失败
           NSLog(@"失败的啦");
        }
    
}

你可能感兴趣的:(微信三方登录(微信原生三方登录))