最新集成微信、QQ登录

1、QQ登录

导入SDK、设置URL types 以及Schemes就不说了,这边只有导入成功后,调起的代码:
AppDelegate.m
记得导入#import

 //9.0之后
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary *)options
{
     if ([url.host isEqualToString:@"oauth"])//微信登录
    {
        return [WXApi handleOpenURL:url delegate:self];
    }
    else if ([url.host isEqualToString:@"qzapp"])//QQ登录
   {
        return [TencentOAuth HandleOpenURL:url];
   }
    return YES;
}
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
    if ([url.host isEqualToString:@"oauth"])//微信登录
    {
        return [WXApi handleOpenURL:url delegate:self];
    }
    else if ([url.host isEqualToString:@"qzapp"])
    {
        return [TencentOAuth HandleOpenURL:url];
    }
    return YES;
}

QQ登录按钮:
记得导入#import 以及遵守TencentSessionDelegate协议
声明 TencentOAuth为全局,如果没有声明为全局可能在回调代理方法时不走。

TencentOAuth * tencentOAuth;

按钮方法中调起QQ:

tencentOAuth = [[TencentOAuth alloc] initWithAppId:qqAppid andDelegate:self];
    NSArray * permissions = [NSArray arrayWithObjects:@"get_user_info",@"get_simple_userinfo", @"add_t", nil];
[tencentOAuth authorize:permissions];
 //实现TencentSessionDelegate代理:
- (void)tencentDidLogin{
    if (tencentOAuth.accessToken && 0 != [tencentOAuth.accessToken length])
    {
        //  记录登录用户的OpenID、Token以及过期时间
        [tencentOAuth getUserInfo];
    }
    else
    {
        NSLog(@"登录不成功 没有获取accesstoken");
    }}
- (void)getUserInfoResponse:(APIResponse *)response {
      // 获取用户信息
    if (response && response.retCode == URLREQUEST_SUCCEED) {
        NSDictionary *userInfo = [response jsonResponse];
        NSString *nickName = userInfo[@"nickname"];
        // 后续操作...
    } else {
        NSLog(@"QQ auth fail ,getUserInfoResponse:%d", response.detailRetCode);
    }
 }

2、微信登录
与QQ一样在Applegate.m文件中,指定返回路径在QQ登录那边就有加判断了,微信登录的代码:

 #import "WXApi.h"

按钮方法:

 //构造SendAuthReq结构体
    SendAuthReq* req = [[SendAuthReq alloc]init];
    req.scope = @"snsapi_userinfo" ;
    req.state = @"app" ;//记得更改标识
    //第三方向微信终端发送一个SendAuthReq消息结构
    [WXApi sendReq:req];

在Applegate.m文件中实现微信回调方法,当然记得遵守微信的协议WXApiDelegate:

#pragma mark - 微信
- (void)onResp:(BaseResp *)resp{
    if ([resp isKindOfClass:[SendAuthResp class]])//授权
    {
        SendAuthResp *temp = (SendAuthResp *)resp;
        NSString *accessUrlStr = [NSString stringWithFormat:@"%@oauth2/access_token?appid=%@&secret=%@&code=%@&grant_type=authorization_code", wxAppUrl,wxAppid, wxAppSecret, temp.code];
        AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
        manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
      //请求accessToken、openID
        [manager GET:accessUrlStr parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSString* accessToken=[responseObject valueForKey:@"access_token"];
            NSString* openID=[responseObject valueForKey:@"openid"];
            NSString *userUrlStr = [NSString stringWithFormat:@"%@userinfo?access_token=%@&openid=%@", wxAppUrl, accessToken, openID];
            AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
            //请求个人信息
            manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
            [manager GET:userUrlStr parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            //返回个人信息
            //处理返回信息,可用通知传到登录界面
            } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            }];
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        }];
    }
}

总的就这些,就是自己处理比较麻烦一点,不像第三方那样直接一句话调用,需要自己处理,当然会弄之后可以自己封装一个。

你可能感兴趣的:(最新集成微信、QQ登录)