ios 三方登錄(qq、微信)

qq三方登錄

1.前往騰訊開(kāi)放平臺(tái)注冊(cè)帳號(hào)并創(chuàng)建應(yīng)用提交審核
1.1 創(chuàng)建應(yīng)用(帳號(hào)的注冊(cè)此處不做說(shuō)明)
1.1
1.2 選擇iOS,并留意APP ID;APP KEY(后面需要用到)
1.2
1.3 填寫(xiě)相應(yīng)的信息并保存、提交(一般來(lái)說(shuō)這個(gè)審核幾個(gè)小時(shí)內(nèi)就會(huì)通過(guò))
1.3
1.4 審核通過(guò)后->管理中心->點(diǎn)擊已獲取QQ登錄能力的應(yīng)用->點(diǎn)擊QQ登錄->填寫(xiě)調(diào)試者qq
1.41

1.42
2. 導(dǎo)入SDK: 點(diǎn)我下載 ,并進(jìn)行相關(guān)配置
2.1 下載后將TencentOpenAPI.framework和TencentOpenApi_iOS_Bundle.bundle拖入你的項(xiàng)目
2.1
2.2 添加依賴庫(kù)

點(diǎn)擊Project navigator 點(diǎn)擊TARGETS -> General -> Linked Frameworks and Libraries->點(diǎn)擊加號(hào)添加

2.2

需要添加的依賴庫(kù):

”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 修改配置屬性

點(diǎn)擊Project navigator 點(diǎn)擊TARGETS -> Build Settings ->Linking->Other Linker Flags->點(diǎn)擊加號(hào)添加屬性值“-fobjc-arc”


2.3
2.4 添加URL Scheme

點(diǎn)擊Project navigator 點(diǎn)擊TARGETS ->info ->URL type-> 添加URL type
Identifier 填寫(xiě):tencentopenapi
URL Scheme填寫(xiě): tencent +APP ID(APP ID: 從上文1.2中科獲得)
?? :你的APP ID是1234567 則填入tencent1234567

2.4

2.5 ios9以后,需要添加白名單

在info.plist文件中加入 LSApplicationQueriesSchemes


2.5
2.6 針對(duì)iOS9默認(rèn)使用https,現(xiàn)在先還原成http請(qǐng)求方式

第一步:在plist中添加NSAppTransportSecurity項(xiàng),此項(xiàng)為NSDictionary
第二步:在NSAppTransportSecurity下添加 NSAllowsArbitraryLoads類型為Boolean,value為YES

2.6
3.代碼區(qū)
3.1 在Appdelegate.m中

導(dǎo)入并重寫(xiě)兩個(gè)方法

#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三方登錄
// 對(duì)外的api: 即點(diǎn)擊qq三方登錄按鈕,調(diào)這個(gè)方法
- (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];
        
        //  設(shè)置需要的權(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以及過(guò)期時(shí)間
        [tencentOAuth getUserInfo];
    }
    else {
        [self failureWith:@"未授權(quán)成功"];
    }
}

//非網(wǎng)絡(luò)錯(cuò)誤導(dǎo)致登錄失敗:
-(void)tencentDidNotLogin:(BOOL)cancelled
{
    if (cancelled){
        [self failureWith:@"用戶取消登錄"];
    }else{
        [self failureWith:@"登錄失敗"];
    }
}
// 網(wǎng)絡(luò)錯(cuò)誤導(dǎo)致登錄失敗:
-(void)tencentDidNotNetWork {
    [self failureWith:@"網(wǎng)絡(luò)錯(cuò)誤"];
}

- (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);
}
補(bǔ)充:
登陸成功的方法里面調(diào)用
       [tencentOAuth getUserInfo];
然后系統(tǒng)會(huì)調(diào)用一個(gè)方法(我們需要提前實(shí)現(xiàn))
-(void)getUserInfoResponse:(APIResponse *)response {
}

在getUserInfoResponse中可以得到所需要的用戶信息

微信三方登錄

1.前往微信開(kāi)放平臺(tái)注冊(cè)帳號(hào)并創(chuàng)建應(yīng)用提交審核

在微信開(kāi)放平臺(tái)注冊(cè)開(kāi)發(fā)者帳號(hào)并創(chuàng)建應(yīng)用,審核通過(guò)后,獲得相應(yīng)的AppID和AppSecret,基本流程和qq三方登錄類似(這里不做說(shuō)明)

2. 導(dǎo)入SDK: 點(diǎn)我下載 ,并進(jìn)行相關(guān)配置
2.1 下載后將下面文件導(dǎo)入工程目錄中
2.1
2.2 添加依賴庫(kù)
2.2

需要添加的依賴庫(kù):

“SystemConfiguration.framework”;
“CoreTelephony.framework”;
“l(fā)ibsqlite3.0.tbd”;
“l(fā)ibstdc++.tbd”;
“l(fā)ibz.tbd”;
"libWeChatSDK.a"
2.3 添加URL Scheme

點(diǎn)擊Project navigator 點(diǎn)擊TARGETS ->info ->URL type-> 添加URL type
Identifier 填寫(xiě):可自定義
URL Scheme填寫(xiě): APP ID(APP ID: 從上文1.2中科獲得)
?? :你的APP ID是wx1234567 則填入wx1234567


2.3
2.4 ios9以后,需要添加白名單

在info.plist文件中加入 LSApplicationQueriesSchemes

2.4
2.5 針對(duì)iOS9默認(rèn)使用https,現(xiàn)在先還原成http請(qǐng)求方式

第一步:在plist中添加NSAppTransportSecurity項(xiàng),此項(xiàng)為NSDictionary
第二步:在NSAppTransportSecurity下添加 NSAllowsArbitraryLoads類型為Boolean,value為YES


2.6
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;
}

// 這個(gè)方法是用于從微信返回第三方App
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
    //  這里我的代理設(shè)置的是ThirdLogin對(duì)象,有的直接設(shè)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  微信三方登錄
//  對(duì)外的api: 即點(diǎn)擊微信三方登錄按鈕,調(diào)這個(gè)方法
- (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)請(qǐng)求過(guò)微信授權(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(@"請(qǐng)求reAccess的response = %@", responseObject);
            NSDictionary *refreshDict = [NSDictionary dictionaryWithDictionary:responseObject];
            NSString *reAccessToken = [refreshDict objectForKey:WX_ACCESS_TOKEN];
            // 如果reAccessToken為空,說(shuō)明reAccessToken也過(guò)期了,反之則沒(méi)有過(guò)期
            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不為空說(shuō)明未超時(shí),直接執(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:@"請(qǐng)求失敗"];
        }];
    }
    else {
        //  本地保存的accessToken、openID為空,說(shuō)明是第一次登陸,或者數(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];
    // 請(qǐng)求用戶數(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:@"請(qǐng)求失敗"];
    }];
}

/*====================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]; // 命令直接同步到文件里,來(lái)避免數(shù)據(jù)的丟失
        }
        [self wechatGetUserInfo];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [self failureWith:@"請(qǐng)求失敗"];
    }];
}
補(bǔ)充:
     access_token   接口調(diào)用憑證 
     refresh_token  用戶刷新access_token 
     openid 授權(quán)用戶唯一標(biāo)識(shí)

access_token有效期為2小時(shí),當(dāng)再次登陸時(shí)通過(guò)refresh_token進(jìn)行刷新,有兩種情況:

  1. access_token未超時(shí),access_token不會(huì)改變,但超時(shí)時(shí)間會(huì)刷新,相當(dāng)于續(xù)期access_token。
  2. 超時(shí),那么會(huì)獲取一個(gè)新的access_token,新的超時(shí)時(shí)間;
注:refresh_token擁有有效期:30天

Comments

如有錯(cuò)誤,望指正

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容