1. 程式人生 > >iOS QQ第三方登入

iOS QQ第三方登入

二.配置工程

1.匯入庫檔案:

SystemConfiguration.framework
Security.framework
CoreTelephony.framework
CoreGraphics.Framework
libiconv.tbd
libsqlite3.tbd
libstdc++.tbd
libz.tbd






2. 修改必要的工程配置屬性:

在工程配置中的“Build Settings”一欄中找到“Linking”配置區,給“Other Linker Flags”配置項新增屬性值“-fobjc-arc”


3.Info.Plist設定

在工程配置中的"Info"中找到"URL Types",新增一條新的“URL scheme”.
注意:

Identifier: tencentopenapi
URL Schemes: tencent + appid
其中Identifier 和URL Schemes是必填項。Identifier是tencentopenapi,URL Schemes是tencent加上你在官網申請的appid

4.針對ios9以後,需要新增白名單

找到工程的Info.plist,然後新增"LSApplicationQueriesSchemes"

<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>

三.整合程式碼

在AppDelegate.m中:

#import "AppDelegate.h"
#import <TencentOpenAPI/QQApiInterface.h>
#import <TencentOpenAPI/TencentOAuth.h>

@interface AppDelegate ()<QQApiInterfaceDelegate>

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    return YES;
}

-(BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options{
    /**
     處理由手Q喚起的跳轉請求
     \param url 待處理的url跳轉請求
     \param delegate 第三方應用用於處理來至QQ請求及響應的委託物件
     \return 跳轉請求處理結果,YES表示成功處理,NO表示不支援的請求協議或處理失敗
     */
    [QQApiInterface handleOpenURL:url delegate:self];
    return [TencentOAuth HandleOpenURL:url];
}


/**
 處理來至QQ的請求
 */
- (void)onReq:(QQBaseReq *)req{
    NSLog(@" ----req %@",req);
}

/**
 處理來至QQ的響應
 */
- (void)onResp:(QQBaseResp *)resp{
    NSLog(@" ----resp %@",resp);
}

/**
 處理QQ線上狀態的回撥
 */
- (void)isOnlineResponse:(NSDictionary *)response{
    
}

在你登入的類中:

#define APP_ID @"你獲得的appid"

#import "ViewController.h"
#import <TencentOpenAPI/TencentOAuth.h>
#import <TencentOpenAPI/TencentApiInterface.h>

@interface ViewController ()<TencentSessionDelegate>
{
    TencentOAuth *_tencentOAuth;
    NSMutableArray *_permissionArray;   //許可權列表
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (IBAction)QQLogin:(id)sender {
    _tencentOAuth=[[TencentOAuth alloc]initWithAppId:APP_ID andDelegate:self];
    
    //設定許可權資料 , 具體的許可權名,在sdkdef.h 檔案中檢視。
    _permissionArray = [NSMutableArray arrayWithObjects: kOPEN_PERMISSION_GET_SIMPLE_USER_INFO,nil];
    
    //登入操作
    [_tencentOAuth authorize:_permissionArray inSafari:NO];
}

/**
 * 登入成功後的回撥
 */
- (void)tencentDidLogin{
    
    /** Access Token憑證,用於後續訪問各開放介面 */
    if (_tencentOAuth.accessToken) {
        
        //獲取使用者資訊。 呼叫這個方法後,qq的sdk會自動呼叫
        //- (void)getUserInfoResponse:(APIResponse*) response
        //這個方法就是 使用者資訊的回撥方法。
        
        [_tencentOAuth getUserInfo];
    }else{
        
        NSLog(@"accessToken 沒有獲取成功");
    }
    
}

/**
 * 登入失敗後的回撥
 * \param cancelled 代表使用者是否主動退出登入
 */
- (void)tencentDidNotLogin:(BOOL)cancelled{
    if (cancelled) {
        NSLog(@" 使用者點選取消按鍵,主動退出登入");
    }else{
        NSLog(@"其他原因, 導致登入失敗");
    }
}

/**
 * 登入時網路有問題的回撥
 */
- (void)tencentDidNotNetWork{
    NSLog(@"沒有網路了, 怎麼登入成功呢");
}

/**
 * 因使用者未授予相應許可權而需要執行增量授權。在使用者呼叫某個api介面時,如果伺服器返回操作未被授權,則觸發該回調協議介面,由第三方決定是否跳轉到增量授權頁面,讓使用者重新授權。
 * \param tencentOAuth 登入授權物件。
 * \param permissions 需增量授權的許可權列表。
 * \return 是否仍然回撥返回原始的api請求結果。
 * \note 不實現該協議介面則預設為不開啟增量授權流程。若需要增量授權請呼叫\ref TencentOAuth#incrAuthWithPermissions: \n注意:增量授權時使用者可能會修改登入的帳號
 */
- (BOOL)tencentNeedPerformIncrAuth:(TencentOAuth *)tencentOAuth withPermissions:(NSArray *)permissions{
    
    // incrAuthWithPermissions是增量授權時需要呼叫的登入介面
    // permissions是需要增量授權的許可權列表
    [tencentOAuth incrAuthWithPermissions:permissions];
    return NO; // 返回NO表明不需要再回傳未授權API介面的原始請求結果;
    // 否則可以返回YES
}

/**
 * [該邏輯未實現]因token失效而需要執行重新登入授權。在使用者呼叫某個api介面時,如果伺服器返回token失效,則觸發該回調協議介面,由第三方決定是否跳轉到登入授權頁面,讓使用者重新授權。
 * \param tencentOAuth 登入授權物件。
 * \return 是否仍然回撥返回原始的api請求結果。
 * \note 不實現該協議介面則預設為不開啟重新登入授權流程。若需要重新登入授權請呼叫\ref TencentOAuth#reauthorizeWithPermissions: \n注意:重新登入授權時使用者可能會修改登入的帳號
 */
- (BOOL)tencentNeedPerformReAuth:(TencentOAuth *)tencentOAuth{
    return YES;
}

/**
 * 使用者通過增量授權流程重新授權登入,token及有效期限等資訊已被更新。
 * \param tencentOAuth token及有效期限等資訊更新後的授權例項物件
 * \note 第三方應用需更新已儲存的token及有效期限等資訊。
 */
- (void)tencentDidUpdate:(TencentOAuth *)tencentOAuth{
    NSLog(@"增量授權完成");
    if (tencentOAuth.accessToken
        && 0 != [tencentOAuth.accessToken length])
    { // 在這裡第三方應用需要更新自己維護的token及有效期限等資訊
        // **務必在這裡檢查使用者的openid是否有變更,變更需重新拉取使用者的資料等資訊** _labelAccessToken.text = tencentOAuth.accessToken;
    }
    else
    {
        NSLog(@"增量授權不成功,沒有獲取accesstoken");
    }
    
}

/**
 * 使用者增量授權過程中因取消或網路問題導致授權失敗
 * \param reason 授權失敗原因,具體失敗原因參見sdkdef.h檔案中\ref UpdateFailType
 */
- (void)tencentFailedUpdate:(UpdateFailType)reason{
    
    switch (reason)
    {
        case kUpdateFailNetwork:
        {
            //            [email protected]"增量授權失敗,無網路連線,請設定網路";
            NSLog(@"增量授權失敗,無網路連線,請設定網路");
            break;
        }
        case kUpdateFailUserCancel:
        {
            //            [email protected]"增量授權失敗,使用者取消授權";
            NSLog(@"增量授權失敗,使用者取消授權");
            break;
        }
        case kUpdateFailUnknown:
        default:
        {
            NSLog(@"增量授權失敗,未知錯誤");
            break;
        }
    }
    
    
}

/**
 * 獲取使用者個人資訊回撥
 * \param response API返回結果,具體定義參見sdkdef.h檔案中\ref APIResponse
 * \remarks 正確返回示例: \snippet example/getUserInfoResponse.exp success
 *          錯誤返回示例: \snippet example/getUserInfoResponse.exp fail
 */
- (void)getUserInfoResponse:(APIResponse*) response{
    NSLog(@" response %@",response);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end