一脊岳、微信登錄
1.根據(jù)微信Api文檔,導(dǎo)入需要的庫(kù)
2.在AppDelegate初始化
[WXApi registerApp:@"appId" withDescription:@"Description"];
3.在需要授權(quán)登錄的事件中,發(fā)送信息給微信
//構(gòu)造SendAuthReq結(jié)構(gòu)體
if([WXApi isWXAppInstalled]){//判斷是否安裝了客戶端
? ? ?SendAuthReq* req =[[SendAuthReq alloc] init];
? ? ?req.scope = @"snsapi_userinfo";//一般都填寫snsapi_userinfo
? ? req.state = @"wexchat" ;//自定義
? ? [WXApi sendReq:req];
}
4.發(fā)送成功后剑刑,微信會(huì)在- (void)onResp:(BaseResp *)resp方法中回調(diào)信息
- (void)onResp:(BaseResp *)resp {
? ? ? ? ?if? ([resp isKindOfClass:[SendAuthResp class]]){
? ? ? ? ? ? ? SendAuthResp *authResp = (SendAuthResp *)rest;
? ? ? ? ? ? ? ?if(authResp.errCode==0){//授權(quán)成功
? ? ? ? ? ? ? ? ? ? ? [self login:authResp.code];
? ? ? ? ? ? ? ? ?}
? ? ? ? ? ?}
}
ERR_OK = 0(用戶同意)
ERR_AUTH_DENIED = -4(用戶拒絕授權(quán))
ERR_USER_CANCEL = -2(用戶取消)
5.成功后沉眶,在成功方法中根據(jù)返回的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(@"請(qǐng)求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來(lái)更新accessToken時(shí)出錯(cuò) = %@", error);
}];
6.最后根據(jù)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(@"請(qǐng)求用戶信息 = %@", responseObject);
NSDictionary *refreshDict = [NSDictionary dictionaryWithDictionary:responseObject];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"獲取用戶信息失敗 = %@", error);
}];
二、QQ登錄(http://wiki.connect.qq.com/sdk下載)
1.根據(jù)文檔導(dǎo)入庫(kù)填渠,如果是基礎(chǔ)功能弦聂,只需導(dǎo)入基礎(chǔ)包
identifier:tencentopenapi ? ? ? url Schemes:tencent+appid
到這里只是打開qq網(wǎng)頁(yè)登錄界面,如果要打開跳轉(zhuǎn)到客戶端氛什,必須在info添加
<key>LSApplicationQueriesSchemes</key>
?<array>
? <string>mqqapi</string>
? <string>mqq</string>
?<string>mqqOpensdkSSoLogin</string>
? <string>mqqconnect</string>
? <string>mqqopensdkdataline<string>
? <string>mqqopensdkgrouptribeshare</string>
? <string>mqqopensdkfriend</string>
? <string>mqqopensdkapi</string>
? <string>mqqopensdkapiV2</string>
? <string>mqqopensdkapiV3</string>
? <string>mqzoneopensdk</string>
? <string>mqqopensdkapiV3</string>
? <string>mqqopensdkapiV3</string>
? <string>mqzone</string>
? <string>mqzonev2</string>
? <string>mqzoneshare</string>
? <string>wtloginqzone</string>
? <string>mqzonewx</string>
? <string>mqzoneopensdkapiV2</string>
? <string>mqzoneopensdkapi19</string>
? <string>mqzoneopensdkapi</string>?
? <string>mqzoneopensdk</string>
?</array>
2.在需要授權(quán)登錄的事件中莺葫,發(fā)送信息給Tencent
if([TencentOAuth iphoneQQInstalled]){//判斷是否安裝了客戶端
? ? ?TencentOAuth *tencentOAuth=? [[TencentOAuth alloc]initWithAppId:qqAppId andDelegate:self]; //delegate必須實(shí)現(xiàn)
? ? NSArray *permissions= @[@"get_user_info",@"get_simple_userinfo",@"add_t"];
? ?[tencentOAuth authorize:permissions localAppId:qqAppId inSafari:NO];
}
實(shí)現(xiàn)TencentSessionDelegate
//授權(quán)成功
-(void)tencentDidLogin{
? ?if (tencentOAuth.accessToken.length!=0)
? ?{
//? 記錄登錄用戶的OpenID、Token以及過(guò)期時(shí)間 ??
? ?kNSLog(@"Tencent用戶授權(quán)成功,Token=%@,openId=%@",tencentOAuth.accessToken,tencentOAuth.openId);
}
else
? {
? ?kNSLog(@"Tencent授權(quán)失敗");
}
}
//非網(wǎng)絡(luò)錯(cuò)誤導(dǎo)致登錄失斍姑肌:
-(void)tencentDidNotLogin:(BOOL)cancelled{
? ?if (cancelled){
? ? ? NSLog(@"Tencent用戶取消登錄");
? }else{
? ? ?NSLog(@"Tencent用戶登錄失敗");
? }
}
// 網(wǎng)絡(luò)錯(cuò)誤導(dǎo)致登錄失斵嗝省:
-(void)tencentDidNotNetWork
{
? ?NSLog(@"tencentDidNotNetWork");
}