iOS 微信 新浪 qq 第三方登录

实现思路: 

微信,微博和qq统称为第三方或第三方平台

app应用登录第三方大致流程是这样:

  1. 获取code
  2. 获取author_token
  3. 获取openid(即用户在该第三方平台的唯一用户标识)(我们的后台)
  4. 根据openid创建或查询用户,返回app用户信息(我们的后台)
  5. 余下和正常的用户注册、登录流程相同

其中qq和sina的第三方登录,手机端必须在获取author_token后,才访问服务器端接口,此时的“codeOrToken”的含义是author_token;weixin的第三方登录,手机端必须在获取code后就访问服务器端接口,此时的“codeOrToken”的含义是code

channel是渠道来源,self代表我们自己的渠道,例如放在百度中,则是百度的渠道(仅和安卓相关)

返回中,通过pass的值来判断究竟是在我们的平台中新建了一个用户还是以前第三方登录的用户,如果是新建,则pass不为空,如果不是新建,则pass=""。返回的token,只有是weixin的情况下才有返回。

如果大家在调用第三方平台的其他接口是需要openid,则请大家查找返回的user中的openid和openSource。

代码实现:

    [WeiboSDKenableDebugMode:NO];

    [WeiboSDKregisterApp:@"appkey"];

    [WXApiregisterApp:@"appkey"];


- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url

{

    return [WXApihandleOpenURL:url delegate:self] || [WeiboSDKhandleOpenURL:url delegate:self] ||[TencentOAuth HandleOpenURL:url];

}


- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation

{

    return [WXApihandleOpenURL:url delegate:self] || [WeiboSDKhandleOpenURL:url delegate:self] ||[TencentOAuth HandleOpenURL:url];

}


- (void)didReceiveWeiboRequest:(WBBaseRequest *)request

{

    

}

- (void)didReceiveWeiboResponse:(WBBaseResponse *)response{

    NSDictionary *dic = [NSDictionarydictionary];

    

    if ([responseisKindOfClass:WBAuthorizeResponse.class])

    {

       if ((int)response.statusCode ==0) {

            dic =@{@"userID":[(WBAuthorizeResponse *)responseuserID],

                   @"accessToken" :[(WBAuthorizeResponse *)responseaccessToken]};        }

    }

   NSLog(@"%@", dic);

    NSString * codeStr = [NSStringstringWithFormat:kUrl, str];

    AFHTTPSessionManager *man = [AFHTTPSessionManagermanager];

    [man.responseSerializersetAcceptableContentTypes:[NSSetsetWithObjects:@"text/plain",nil]];

    [manGET:codeStr parameters:nilsuccess:^(NSURLSessionDataTask *_Nonnull task, id _Nullable responseObject) {

        

       NSLog(@"%@", responseObject);


    }failure:^(NSURLSessionDataTask *_Nullable task, NSError *_Nonnull error) {

       NSLog(@"%@", error);

    }];




}


- (void)onReq:(BaseReq*)req

{

   /*

     onReq是微信终端向第三方程序发起请求,要求第三方程序响应。第三方程序响应完后必须调用sendRsp返回。在调用sendRsp返回时,会切回到微信终端程序界面。

     */

}


- (void)onResp:(BaseResp*)resp

{

   /*

     如果第三方程序向微信发送了sendReq的请求,那么onResp会被回调。sendReq请求调用后,会切到微信终端程序界面。

     */

    SendAuthResp *aresp = (SendAuthResp *)resp;

   if (aresp.errCode==0) {

        

       NSString *code = aresp.code;

       NSDictionary *dic = @{@"code":code};

        

        

        NSString * codeStr = [NSStringstringWithFormat:kUrl, str];

       NSLog(@"%@", codeStr);

        AFHTTPSessionManager *man = [AFHTTPSessionManagermanager];

        [man.responseSerializersetAcceptableContentTypes:[NSSetsetWithObjects:@"text/plain",nil]];

        [manGET:codeStr parameters:nilsuccess:^(NSURLSessionDataTask *_Nonnull task, id _Nullable responseObject) {

            

           NSLog(@"%@", responseObject);

            

        }failure:^(NSURLSessionDataTask *_Nullable task, NSError *_Nonnull error) {

           NSLog(@"%@", error);

        }];

    }

}


//微博登录

- (void)sinaBAction

{

    WBAuthorizeRequest *request = [WBAuthorizeRequestrequest];

    request.redirectURI =@"授权页";

    request.scope =@"all";

    [WeiboSDKsendRequest:request];

}


//qq登录

- (void)qqBAction

{

    self.auth=[[TencentOAuthalloc]initWithAppId:@"appkey"andDelegate:self];

    

    NSArray * permissions= [NSArrayarrayWithObjects:@"get_user_info",@"get_simple_userinfo",@"add_t",nil];

    

    [self.authauthorize:permissions inSafari:NO];

}


//登陆完成调用

- (void)tencentDidLogin

{

    if (self.auth.accessToken && 0 != self.auth.accessToken.length)

    {

        //  记录登录用户的OpenIDToken以及过期时间

        NSString * codeStr = [NSStringstringWithFormat:kUrl, str];

        AFHTTPSessionManager *man = [AFHTTPSessionManagermanager];

        [man.responseSerializersetAcceptableContentTypes:[NSSetsetWithObjects:@"text/plain",nil]];

        [manGET:codeStr parameters:nilsuccess:^(NSURLSessionDataTask *_Nonnull task, id _Nullable responseObject) {

           NSLog(@"%@", responseObject);

           self.userInfo = [[responseObjectobjectForKey:@"result"]objectForKey:@"user"];

            [selfdismissViewControllerAnimated:YEScompletion:^{

                [[NSNotificationCenterdefaultCenter]postNotificationName:KLoginAndRegistFinishedNotificationKey

                                                                   object:nil

                                                                 userInfo:self.userInfo];

            }];

        }failure:^(NSURLSessionDataTask *_Nullable task, NSError *_Nonnull error) {

           NSLog(@"%@", error);

        }];

    }

   else

    {

       NSLog(@"登陆失败");

    }

    

    

}

//非网络错误导致登录失败:

-(void)tencentDidNotLogin:(BOOL)cancelled

{

   NSLog(@"登录失败");

}

// 网络错误导致登录失败:

-(void)tencentDidNotNetWork

{

    NSLog(@"无网络连接,请设置网络");

}


//微信登录

- (void)wxBAction

{

    SendAuthReq* req =[[SendAuthReqalloc ] init];

    req.scope =@"snsapi_userinfo";

    req.state =@"weixin";

    [WXApisendReq:req];

}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值