iOS 第三方授权登录

一、微信登录

1.根据微信Api文档,导入需要的库

iOS 第三方授权登录_第1张图片

2.在AppDelegate初始化

[WXApi registerApp:@"appId" withDescription:@"Description"];

3.在需要授权登录的事件中,发送信息给微信

//构造SendAuthReq结构体

if([WXApi isWXAppInstalled]){//判断是否安装了客户端

     SendAuthReq* req =[[SendAuthReq alloc] init];

     req.scope = @"snsapi_userinfo";//一般都填写snsapi_userinfo

    req.state = @"wexchat" ;//自定义

    [WXApi sendReq:req];

}

4.发送成功后,微信会在- (void)onResp:(BaseResp *)resp方法中回调信息

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

         if  ([resp isKindOfClass:[SendAuthResp class]]){

              SendAuthResp *authResp = (SendAuthResp *)rest;

               if(authResp.errCode==0){//授权成功

                      [self login:authResp.code];

                 }

           }

}

ERR_OK = 0(用户同意)

ERR_AUTH_DENIED = -4(用户拒绝授权)

ERR_USER_CANCEL = -2(用户取消)

5.成功后,在成功方法中根据返回的code获取Token和openID

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/html", nil];

NSString *refreshUrlStr = [NSString stringWithFormat:@"%@/oauth2/access_token?appid=%@&secret=%@&code=%@&grant_type=authorization_code",WX_BASE_URL, wxAppId, wxSecret,authResp.code];

[manager GET:refreshUrlStr parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {

     NSLog(@"请求reAccess的response = %@", responseObject);

    NSDictionary *accessDict = [NSDictionary dictionaryWithDictionary:responseObject];

   NSString *accessToken = [accessDict objectForKey:WX_ACCESS_TOKEN];

   NSString *openID = [accessDict objectForKey:WX_OPEN_ID];

    NSString *refreshToken = [accessDict objectForKey:WX_REFRESH_TOKEN];

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

       NSLog(@"用refresh_token来更新accessToken时出错 = %@", error);

}];

6.最后根据Token 和 openId获取到用户的信息

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/html", nil];

NSString *url = [NSString stringWithFormat:@"%@/userinfo?access_token=%@&openid=%@",WX_BASE_URL,access_token,openid];

[manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {

NSLog(@"请求用户信息 = %@", responseObject);

NSDictionary *refreshDict = [NSDictionary dictionaryWithDictionary:responseObject];

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

NSLog(@"获取用户信息失败 = %@", error);

}];


二、QQ登录(http://wiki.connect.qq.com/sdk下载)

1.根据文档导入库,如果是基础功能,只需导入基础包

identifier:tencentopenapi       url Schemes:tencent+appid

到这里只是打开qq网页登录界面,如果要打开跳转到客户端,必须在info添加

LSApplicationQueriesSchemes

 

  mqqapi

  mqq

 mqqOpensdkSSoLogin

  mqqconnect

  mqqopensdkdataline

  mqqopensdkgrouptribeshare

  mqqopensdkfriend

  mqqopensdkapi

  mqqopensdkapiV2

  mqqopensdkapiV3

  mqzoneopensdk

  mqqopensdkapiV3

  mqqopensdkapiV3

  mqzone

  mqzonev2

  mqzoneshare

  wtloginqzone

  mqzonewx

  mqzoneopensdkapiV2

  mqzoneopensdkapi19

  mqzoneopensdkapi 

  mqzoneopensdk

 


2.在需要授权登录的事件中,发送信息给Tencent

if([TencentOAuth iphoneQQInstalled]){//判断是否安装了客户端

     TencentOAuth *tencentOAuth=  [[TencentOAuth alloc]initWithAppId:qqAppId andDelegate:self]; //delegate必须实现

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

   [tencentOAuth authorize:permissions localAppId:qqAppId inSafari:NO];

}

实现TencentSessionDelegate

//授权成功

-(void)tencentDidLogin{

   if (tencentOAuth.accessToken.length!=0)

   {

//  记录登录用户的OpenID、Token以及过期时间   

   kNSLog(@"Tencent用户授权成功,Token=%@,openId=%@",tencentOAuth.accessToken,tencentOAuth.openId);

}

else

  {

   kNSLog(@"Tencent授权失败");

}

}

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

-(void)tencentDidNotLogin:(BOOL)cancelled{

   if (cancelled){

      NSLog(@"Tencent用户取消登录");

  }else{

     NSLog(@"Tencent用户登录失败");

  }

}

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

-(void)tencentDidNotNetWork

{

   NSLog(@"tencentDidNotNetWork");

}

你可能感兴趣的:(iOS 第三方授权登录)