iOS QQ第三方登實現

咱們常常會見到應用登錄的時候會有QQ,微信,微博等的第三方登錄c++

如圖:sql

下面咱們主要講一下qq的第三方登錄如何實現json

首先,到官網註冊:api

http://wiki.connect.qq.com微信

一,下載SDK網絡

下載SDK  地址:app

 http://wiki.open.qq.com/wiki/mobile/SDK下載ide


下載最新版本的iOS_SDK_V2.9測試

二,SDK的目錄結構

下載的文件結構以下網站



---------------------------------------------------------------------------------------------------------------------

sample:示例代碼

1. TencentOpenAPI.framework打包了iOS SDK的頭文件定義和具體實現。

2. TencentOpenApi_iOS_Bundle.bundle 打包了iOS SDK須要的資源文件。


三,在Xcode中建立項目


新建空白項目,起名TestQQLogin






四,將iOS SDK添加到項目中

1. 將iOS SDK中的TencentOpenAPI.framework和TencentOpenApi_IOS_Bundle.bundle文件拖放到應用開發的目錄下。

2,在彈出的框中選擇以下



五,添加依賴庫

點擊Project navigator 點擊TARGETS --->  General  ---> Linked Frameworks and Libraries

點擊加號添加


添加SDK依賴的系統庫文件。分別是

」Security.framework」, 

「libiconv.dylib」,

「SystemConfiguration.framework」,

「CoreGraphics.Framework」、

「libsqlite3.dylib」、

「CoreTelephony.framework」、

「libstdc++.dylib」、

「libz.dylib」。


六, 修改必要的工程配置屬性

1,在工程配置中的「Build Settings」一欄中找到「Linking」配置區,給「Other Linker Flags」配置項添加屬性值「-fobjc-arc」




效果以下圖:



2,在XCode中,選擇你的工程設置項,選中「TARGETS」一欄,在「info」標籤欄的「URL type」添加一條新的「URL scheme」,新的scheme = tencent + appid(例如你的appid是123456 則填入tencent123456) identifier 填寫:tencentopenapi。appid怎麼來請看第七步。




七,在騰訊應用寶建立應用

第六步配置中須要的appid等信息 須要首先在應用寶中建立應用才能獲得。

首先登錄網站:http://open.qq.com

建立應用,在應用詳情中能夠看到appid



申請完成後必定記得添加測試qq,不然沒有審覈經過的應用是沒法直接登錄的



八,開始寫代碼


1,打開剛纔新建的工程,重寫appdelegate的兩個方法




重寫以前導入頭文件 


#import <TencentOpenAPI/TencentOAuth.h>



openURL:


- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{

return [TencentOAuth HandleOpenURL:url];

}


handleOpenURL:

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{

return [TencentOAuth HandleOpenURL:url];

}




2 , 在須要使用的 viewController中 初始化

 tencentOAuth=[[TencentOAuthalloc]initWithAppId:@"你的appid"andDelegate:self];

3,設置權限列表

 //4,設置須要的權限列表,此處儘可能使用什麼取什麼。

    permissions= [NSArrayarrayWithObjects:@"get_user_info",@"get_simple_userinfo",@"add_t",nil];


4,登錄

 [tencentOAuth authorize:permissionsinSafari:NO];



5,在代碼中實現 TencentSessionDelegate 方法


#pragma mark -- TencentSessionDelegate

//登錄完成調用

- (void)tencentDidLogin

{

    resultLable.text =@"登陸完成";

    

    if (tencentOAuth.accessToken &&0 != [tencentOAuth.accessTokenlength])

    {

        //  記錄登陸用戶的OpenIDToken以及過時時間

        tokenLable.text =tencentOAuth.accessToken;

    }

   else

    {

        tokenLable.text =@"登陸不成功沒有獲取accesstoken";

    }

}


//非網絡錯誤致使登陸失敗:

-(void)tencentDidNotLogin:(BOOL)cancelled

{

    NSLog(@"tencentDidNotLogin");

   if (cancelled)

    {

       resultLable.text =@"用戶取消登陸";

    }else{

       resultLable.text =@"登陸失敗";

    }

}
// 網絡錯誤致使登陸失敗:

-(void)tencentDidNotNetWork

{

    NSLog(@"tencentDidNotNetWork");

    resultLable.text =@"無網絡鏈接,請設置網絡";

}


- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


以上方法基本上就實現了登錄,下來咱們得考慮登錄成功以後如何獲取用戶信息

其實方法很簡單咱們在登錄成功的方法裏面調用


        [tencentOAuth getUserInfo];


而後系統會調用一個方法(咱們須要提早實現)

-(void)getUserInfoResponse:(APIResponse *)response

{

    NSLog(@"respons:%@",response.jsonResponse);

}


在getUserInfoResponse中就能夠看到所須要的用用戶信息



大體代碼以下
 
   
<pre name="code" class="objc">#import "ViewController.h"
#import <TencentOpenAPI/TencentOAuth.h>

@interface ViewController ()<TencentSessionDelegate>
{
    UIButton *qqLoginBtn;
    TencentOAuth *tencentOAuth;
    NSArray *permissions;
    UILabel *resultLable;
    UILabel *tokenLable;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    //1,初始化登錄按鈕 添加到當前view中
    qqLoginBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    qqLoginBtn.frame=CGRectMake(100, 50, 36, 36);
    [qqLoginBtn setTitle:@"登錄" forState:UIControlStateNormal];
    [qqLoginBtn addTarget:self action:@selector(loginAct) forControlEvents:UIControlEventTouchDown];
    [self.view addSubview:qqLoginBtn];
    
    //2,初始 lable
    resultLable=[[UILabel alloc]initWithFrame:CGRectMake(30, 100, 200, 36)];
    tokenLable=[[UILabel alloc]initWithFrame:CGRectMake(30, 150, 200, 36)];
    [self.view addSubview:resultLable];
    [self.view addSubview:tokenLable];
    
    //3,初始化TencentOAuth 對象 appid來自應用寶建立的應用, deletegate設置爲self  必定記得實現代理方法
    
    //這裏的appid填寫應用寶獲得的id  記得修改 「TARGETS」一欄,在「info」標籤欄的「URL type」添加 的「URL scheme」,新的scheme。有問題家QQ羣414319235提問
    tencentOAuth=[[TencentOAuth alloc]initWithAppId:@"1104617535" andDelegate:self];
    
    //4,設置須要的權限列表,此處儘可能使用什麼取什麼。
    permissions= [NSArray arrayWithObjects:@"get_user_info", @"get_simple_userinfo", @"add_t", nil];
    
}
#pragma mark -- login
-(void)loginAct
{
    NSLog(@"loginAct");
    [tencentOAuth authorize:permissions inSafari:NO];
}

#pragma mark -- TencentSessionDelegate
//登錄完成調用
- (void)tencentDidLogin
{
    resultLable.text = @"登陸完成";
    
    if (tencentOAuth.accessToken && 0 != [tencentOAuth.accessToken length])
    {
        //  記錄登陸用戶的OpenID、Token以及過時時間
        tokenLable.text = tencentOAuth.accessToken;
        [tencentOAuth getUserInfo];
    }
    else
    {
        tokenLable.text = @"登陸不成功 沒有獲取accesstoken";
    }
}

//非網絡錯誤致使登陸失敗:
-(void)tencentDidNotLogin:(BOOL)cancelled
{
    NSLog(@"tencentDidNotLogin");
    if (cancelled)
    {
        resultLable.text = @"用戶取消登陸";
    }else{
        resultLable.text = @"登陸失敗";
    }
}
// 網絡錯誤致使登陸失敗:
-(void)tencentDidNotNetWork
{
    NSLog(@"tencentDidNotNetWork");
    resultLable.text = @"無網絡鏈接,請設置網絡";
}

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

-(void)getUserInfoResponse:(APIResponse *)response
{
    NSLog(@"respons:%@",response.jsonResponse);
}

@end


 
   

九,真機測試效果


打開登錄界面:

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


登錄中


------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


登陸成功


------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------



示例代碼上傳到qq羣 414319235  你們須要能夠去下載

相關文章
相關標籤/搜索