qq三方登錄
1.前往騰訊開放平臺注冊帳號并創(chuàng)建應用提交審核
1.1 創(chuàng)建應用(帳號的注冊此處不做說明)
1.2 選擇iOS积担,并留意APP ID;APP KEY(后面需要用到)
1.3 填寫相應的信息并保存、提交(一般來說這個審核幾個小時內(nèi)就會通過)
1.4 審核通過后->管理中心->點擊已獲取QQ登錄能力的應用->點擊QQ登錄->填寫調(diào)試者qq
2. 導入SDK: 點我下載 ,并進行相關(guān)配置
2.1 下載后將TencentOpenAPI.framework和TencentOpenApi_iOS_Bundle.bundle拖入你的項目
2.2 添加依賴庫
點擊Project navigator 點擊TARGETS -> General -> Linked Frameworks and Libraries->點擊加號添加
需要添加的依賴庫:
”Security.framework”;
“l(fā)ibiconv.tbd”;
“SystemConfiguration.framework”;
“CoreGraphics.Framework”;
“l(fā)ibsqlite3.tbd”;
“CoreTelephony.framework”;
“l(fā)ibstdc++.tbd”;
“l(fā)ibz.tbd”辞友。
2.3 修改配置屬性
點擊Project navigator 點擊TARGETS -> Build Settings ->Linking->Other Linker Flags->點擊加號添加屬性值“-fobjc-arc”
2.4 添加URL Scheme
點擊Project navigator 點擊TARGETS ->info ->URL type-> 添加URL type
Identifier 填寫:tencentopenapi
URL Scheme填寫: tencent +APP ID(APP ID: 從上文1.2中科獲得)
?? :你的APP ID是1234567 則填入tencent1234567
2.5 ios9以后,需要添加白名單
在info.plist文件中加入 LSApplicationQueriesSchemes
2.6 針對iOS9默認使用https,現(xiàn)在先還原成http請求方式
第一步:在plist中添加NSAppTransportSecurity項震肮,此項為NSDictionary
第二步:在NSAppTransportSecurity下添加 NSAllowsArbitraryLoads類型為Boolean称龙,value為YES
3.代碼區(qū)
3.1 在Appdelegate.m中
導入并重寫兩個方法
#import <TencentOpenAPI/TencentOAuth.h>
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
return [TencentOAuth HandleOpenURL:url];
}
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{
return [TencentOAuth HandleOpenURL:url];
}
3.2 在ThirdLogin.m(用于封裝三方登錄的類)中
#import "ThirdLogin.h"
#import <TencentOpenAPI/TencentOAuth.h>
#import "HttpClient.h"
#define NAME @"name"
#define PICTURE @"picture"
#define OPENID @"openId"
@interface ThirdLogin ()<TencentSessionDelegate>
{
TencentOAuth *tencentOAuth;
NSArray *permissions;
}
@property (copy, nonatomic) void (^success)(NSDictionary *result);
@property (copy, nonatomic) void (^failure)(NSError *error);
@end
@implementation ThirdLogin
#pragma mark--------------------------------------------------------
#pragma mark qq三方登錄
// 對外的api: 即點擊qq三方登錄按鈕,調(diào)這個方法
- (void)qqLoginWithSuccess:(void (^)(NSDictionary *userInfo))success failure:(void (^)(NSError *error))failure {
// 成功或失敗的回調(diào)block
self.success = [success copy];
self.failure = [failure copy];
// 需要放在主線程中執(zhí)行
dispatch_async(dispatch_get_main_queue(), ^{
tencentOAuth=[[TencentOAuth alloc]initWithAppId:@"1105549185" andDelegate:self];
// 設置需要的權(quán)限列表戳晌,此處盡量使用什么取什么鲫尊。
permissions= [NSArray arrayWithObjects:kOPEN_PERMISSION_GET_USER_INFO,
kOPEN_PERMISSION_GET_SIMPLE_USER_INFO, @"add_t", nil];
[tencentOAuth authorize:permissions];
});
}
- (BOOL)tencentNeedPerformIncrAuth:(TencentOAuth *)tencentOAuth withPermissions:(NSArray *)permissions {
return YES;
}
#pragma mark -- TencentSessionDelegate
//登陸完成調(diào)用
- (void)tencentDidLogin
{
if (tencentOAuth.accessToken && 0 != [tencentOAuth.accessToken length]) {
// 記錄登錄用戶的OpenID雨饺、Token以及過期時間
[tencentOAuth getUserInfo];
}
else {
[self failureWith:@"未授權(quán)成功"];
}
}
//非網(wǎng)絡錯誤導致登錄失敳钥贰:
-(void)tencentDidNotLogin:(BOOL)cancelled
{
if (cancelled){
[self failureWith:@"用戶取消登錄"];
}else{
[self failureWith:@"登錄失敗"];
}
}
// 網(wǎng)絡錯誤導致登錄失敗:
-(void)tencentDidNotNetWork {
[self failureWith:@"網(wǎng)絡錯誤"];
}
- (void)tencentDidLogout{
// NSLog(@"登出");
}
-(void)getUserInfoResponse:(APIResponse *)response {
NSDictionary *result = [NSDictionary dictionaryWithObjectsAndKeys:[response.jsonResponse objectForKey:@"nickname"], [response.jsonResponse objectForKey:@"figureurl_qq_2"], tencentOAuth.openId, NAME, PICTURE, OPENID,nil];
self.success(result);
}
- (void)failureWith:(NSString *)domin {
NSError *error = [NSError errorWithDomain:domin code:0 userInfo:nil];
self.failure(error);
}
補充:
登陸成功的方法里面調(diào)用
[tencentOAuth getUserInfo];
然后系統(tǒng)會調(diào)用一個方法(我們需要提前實現(xiàn))
-(void)getUserInfoResponse:(APIResponse *)response {
}
在getUserInfoResponse中可以得到所需要的用戶信息
微信三方登錄
1.前往微信開放平臺注冊帳號并創(chuàng)建應用提交審核
在微信開放平臺注冊開發(fā)者帳號并創(chuàng)建應用商佑,審核通過后豪嚎,獲得相應的AppID和AppSecret搔驼,基本流程和qq三方登錄類似(這里不做說明)
2. 導入SDK: 點我下載 ,并進行相關(guān)配置
2.1 下載后將下面文件導入工程目錄中
2.2 添加依賴庫
需要添加的依賴庫:
“SystemConfiguration.framework”;
“CoreTelephony.framework”;
“l(fā)ibsqlite3.0.tbd”;
“l(fā)ibstdc++.tbd”;
“l(fā)ibz.tbd”;
"libWeChatSDK.a"
2.3 添加URL Scheme
點擊Project navigator 點擊TARGETS ->info ->URL type-> 添加URL type
Identifier 填寫:可自定義
URL Scheme填寫: APP ID(APP ID: 從上文1.2中科獲得)
?? :你的APP ID是wx1234567 則填入wx1234567
2.4 ios9以后侈询,需要添加白名單
在info.plist文件中加入 LSApplicationQueriesSchemes
2.5 針對iOS9默認使用https,現(xiàn)在先還原成http請求方式
第一步:在plist中添加NSAppTransportSecurity項舌涨,此項為NSDictionary
第二步:在NSAppTransportSecurity下添加 NSAllowsArbitraryLoads類型為Boolean,value為YES
3.代碼區(qū)
3.1 在Appdelegate.m中
#import "AppDelegate.h"
#import "WXApi.h"
#import "ThirdLogin.h" // 用于封裝三方登錄的類
@interface AppDelegate ()<WXApiDelegate>
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// @"wx1235467" : APP ID
[WXApi registerApp:@"wx1235467" withDescription:@"Wechat"];
return YES;
}
// 這個方法是用于從微信返回第三方App
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
// 這里我的代理設置的是ThirdLogin對象扔字,有的直接設self(即AppDelegate)
[WXApi handleOpenURL:url delegate:[ThirdLogin shareThirdLogin]];
return YES;
}
3.1 在ThirdLogin.m中
#import "ThirdLogin.h"
#import "HttpClient.h"
#import "WXApi.h"
#define NAME @"name"
#define PICTURE @"picture"
#define OPENID @"openId"
@interface ThirdLogin ()<WXApiDelegate>
@property (copy, nonatomic) void (^success)(NSDictionary *result);
@property (copy, nonatomic) void (^failure)(NSError *error);
@end
#pragma mark--------------------------------------------------------
#pragma mark 微信三方登錄
// 對外的api: 即點擊微信三方登錄按鈕泼菌,調(diào)這個方法
- (void)weixinLoginWithsuccess:(void (^)(NSDictionary *userInfo))success failure:(void (^)(NSError *error))failure {
// 成功或失敗的回調(diào)block
self.success = [success copy];
self.failure = [failure copy];
NSString *accessToken = [[NSUserDefaults standardUserDefaults] objectForKey:WX_ACCESS_TOKEN];
NSString *openID = [[NSUserDefaults standardUserDefaults] objectForKey:WX_OPEN_ID];
// 如果已經(jīng)請求過微信授權(quán)登錄谍肤,那么考慮用已經(jīng)得到的access_token
if (accessToken && openID) {
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSString *refreshToken = [[NSUserDefaults standardUserDefaults] objectForKey:WX_REFRESH_TOKEN];
NSString *refreshUrlStr = [NSString stringWithFormat:@"%@/oauth2/refresh_token?appid=%@&grant_type=refresh_token&refresh_token=%@", WX_BASE_URL, WXPatient_App_ID, refreshToken];
[manager GET:refreshUrlStr parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"請求reAccess的response = %@", responseObject);
NSDictionary *refreshDict = [NSDictionary dictionaryWithDictionary:responseObject];
NSString *reAccessToken = [refreshDict objectForKey:WX_ACCESS_TOKEN];
// 如果reAccessToken為空,說明reAccessToken也過期了,反之則沒有過期
if (reAccessToken) {
// 更新access_token、refresh_token哗伯、open_id
[[NSUserDefaults standardUserDefaults] setObject:reAccessToken forKey:WX_ACCESS_TOKEN];
[[NSUserDefaults standardUserDefaults] setObject:[refreshDict objectForKey:WX_OPEN_ID] forKey:WX_OPEN_ID];
[[NSUserDefaults standardUserDefaults] setObject:[refreshDict objectForKey:WX_REFRESH_TOKEN] forKey:WX_REFRESH_TOKEN];
[[NSUserDefaults standardUserDefaults] synchronize];
// reAccessToken不為空說明未超時荒揣,直接執(zhí)行wechatLoginByRequestForUserInfo方法獲取數(shù)據(jù)
if ([self respondsToSelector:@selector(wechatGetUserInfo)]) {
[self wechatGetUserInfo];
}
}else {
// refresh_token失效的后需重新授權(quán)
[self wechatLogin];
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
[self failureWith:@"請求失敗"];
}];
}
else {
// 本地保存的accessToken、openID為空焊刹,說明是第一次登陸系任,或者數(shù)據(jù)遺失
[self wechatLogin];
}
}
- (void)wechatLogin {
if ([WXApi isWXAppInstalled]) {
SendAuthReq *req = [[SendAuthReq alloc] init];
req.scope = @"snsapi_userinfo";
req.state = @"App";
[WXApi sendReq:req];
}else{
//把微信登錄的按鈕隱藏掉。
}
}
- (void)wechatGetUserInfo {
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSString *accessToken = [[NSUserDefaults standardUserDefaults] objectForKey:WX_ACCESS_TOKEN];
NSString *openID = [[NSUserDefaults standardUserDefaults] objectForKey:WX_OPEN_ID];
NSString *userUrlStr = [NSString stringWithFormat:@"%@/userinfo?access_token=%@&openid=%@", WX_BASE_URL, accessToken, openID];
// 請求用戶數(shù)據(jù)
[manager GET:userUrlStr parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSDictionary *result = [NSDictionary dictionaryWithObjectsAndKeys:responseObject[@"nickname"], responseObject[@"headimgurl"], openID, NAME, PICTURE, OPENID,nil];
self.success(result);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
[self failureWith:@"請求失敗"];
}];
}
/*====================delegate方法=====================*/
-(void) onResp:(BaseResp*)resp{
SendAuthResp *temp = (SendAuthResp *)resp;
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSString *accessUrlStr = [NSString stringWithFormat:@"%@/oauth2/access_token?appid=%@&secret=%@&code=%@&grant_type=authorization_code", WX_BASE_URL, WXPatient_App_ID, WXPatient_App_Secret, temp.code];
[manager GET:accessUrlStr parameters:nil success:^(AFHTTPRequestOperation *operation, id 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];
// 本地持久化虐块,以便access_token的使用俩滥、刷新或者持續(xù)
if (accessToken && ![accessToken isEqualToString:@""] && openID && ![openID isEqualToString:@""]) {
[[NSUserDefaults standardUserDefaults] setObject:accessToken forKey:WX_ACCESS_TOKEN];
[[NSUserDefaults standardUserDefaults] setObject:openID forKey:WX_OPEN_ID];
[[NSUserDefaults standardUserDefaults] setObject:refreshToken forKey:WX_REFRESH_TOKEN];
[[NSUserDefaults standardUserDefaults] synchronize]; // 命令直接同步到文件里,來避免數(shù)據(jù)的丟失
}
[self wechatGetUserInfo];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
[self failureWith:@"請求失敗"];
}];
}
補充:
access_token 接口調(diào)用憑證
refresh_token 用戶刷新access_token
openid 授權(quán)用戶唯一標識
access_token有效期為2小時贺奠,當再次登陸時通過refresh_token進行刷新霜旧,有兩種情況:
- access_token未超時,access_token不會改變儡率,但超時時間會刷新挂据,相當于續(xù)期access_token。
- 超時,那么會獲取一個新的access_token儿普,新的超時時間;
注:refresh_token擁有有效期:30天
Comments
如有錯誤崎逃,望指正