日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

第三方登录之qq登录(转载)

發(fā)布時(shí)間:2024/8/26 40 生活家
生活随笔 收集整理的這篇文章主要介紹了 第三方登录之qq登录(转载) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

iOS QQ第三方登實(shí)現(xiàn)

我們經(jīng)常會(huì)見到應(yīng)用登陸的時(shí)候會(huì)有QQ,微信,微博等的第三方登陸

如圖:

下面我們主要講一下qq的第三方登陸如何實(shí)現(xiàn)

首先,到官網(wǎng)注冊(cè):

http://wiki.connect.qq.com

一,下載SDK

下載SDK 地址:

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

下載最新版本的iOS_SDK_V2.9

二,SDK的目錄結(jié)構(gòu)

下載的文件結(jié)構(gòu)如下

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

sample:示例代碼

1.TencentOpenAPI.framework打包了iOS SDK的頭文件定義和具體實(shí)現(xiàn)。

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

三,在Xcode中創(chuàng)建項(xiàng)目

新建空白項(xiàng)目,起名TestQQLogin

四,將iOS SDK添加到項(xiàng)目中

1. 將iOS SDK中的TencentOpenAPI.framework和TencentOpenApi_IOS_Bundle.bundle文件拖放到應(yīng)用開發(fā)的目錄下。

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

五,添加依賴庫

點(diǎn)擊Project navigator 點(diǎn)擊TARGETS ---> General ---> Linked Frameworks and Libraries

點(diǎn)擊加號(hào)添加

添加SDK依賴的系統(tǒng)庫文件。分別是

”Security.framework”,

“l(fā)ibiconv.dylib”,

“SystemConfiguration.framework”,

“CoreGraphics.Framework”、

“l(fā)ibsqlite3.dylib”、

“CoreTelephony.framework”、

“l(fā)ibstdc++.dylib”、

“l(fā)ibz.dylib”。

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

1,在工程配置中的“Build Settings”一欄中找到“Linking”配置區(qū),給“Other Linker Flags”配置項(xiàng)添加屬性值“-fobjc-arc”

效果如下圖:

2,在XCode中,選擇你的工程設(shè)置項(xiàng),選中“TARGETS”一欄,在“info”標(biāo)簽欄的“URL type”添加一條新的“URL scheme”,新的scheme = tencent + appid(例如你的appid是123456 則填入tencent123456) identifier 填寫:tencentopenapi。appid怎么來請(qǐng)看第七步。

七,在騰訊應(yīng)用寶創(chuàng)建應(yīng)用

第六步配置中需要的appid等信息 需要首先在應(yīng)用寶中創(chuàng)建應(yīng)用才能得到。

首先登陸網(wǎng)站:http://open.qq.com

創(chuàng)建應(yīng)用,在應(yīng)用詳情中可以看到appid

申請(qǐng)完成后一定記得添加測(cè)試qq,否則沒有審核通過的應(yīng)用是無法直接登陸的

八,開始寫代碼

1,打開剛才新建的工程,重寫appdelegate的兩個(gè)方法

重寫之前導(dǎo)入頭文件

#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,設(shè)置權(quán)限列表

//4,設(shè)置需要的權(quán)限列表,此處盡量使用什么取什么。

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

4,登陸

[tencentOAuthauthorize:permissionsinSafari:NO];

5,在代碼中實(shí)現(xiàn) TencentSessionDelegate 方法

#pragma mark -- TencentSessionDelegate

//登陸完成調(diào)用

- (void)tencentDidLogin

{

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

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

{

// 記錄登錄用戶的OpenID、Token以及過期時(shí)間

tokenLable.text=tencentOAuth.accessToken;

}

else

{

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

}

}

//非網(wǎng)絡(luò)錯(cuò)誤導(dǎo)致登錄失敗:

-(void)tencentDidNotLogin:(BOOL)cancelled

{

NSLog(@"tencentDidNotLogin");

if(cancelled)

{

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

}else{

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

}

}
//網(wǎng)絡(luò)錯(cuò)誤導(dǎo)致登錄失敗:

-(void)tencentDidNotNetWork

{

NSLog(@"tencentDidNotNetWork");

resultLable.text=@"無網(wǎng)絡(luò)連接,請(qǐng)?jiān)O(shè)置網(wǎng)絡(luò)";

}

- (void)didReceiveMemoryWarning {

[superdidReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

以上方法基本上就實(shí)現(xiàn)了登陸,下來我們得考慮登陸成功之后如何獲取用戶信息

其實(shí)方法很簡(jiǎn)單我們?cè)诘顷懗晒Φ姆椒ɡ锩嬲{(diào)用

[tencentOAuthgetUserInfo];

然后系統(tǒng)會(huì)調(diào)用一個(gè)方法(我們需要提前實(shí)現(xiàn))

-(void)getUserInfoResponse:(APIResponse*)response

{

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

}

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

大致代碼如下

[objc]view plaincopy

[objc]view plaincopy

<prename="code"class="objc">#import"ViewController.h"
#import<TencentOpenAPI/TencentOAuth.h>

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

@end

@implementationViewController

-(void)viewDidLoad{
[superviewDidLoad];
//Doanyadditionalsetupafterloadingtheview,typicallyfromanib.

//1,初始化登陸按鈕添加到當(dāng)前view中
qqLoginBtn=[UIButtonbuttonWithType:UIButtonTypeRoundedRect];
qqLoginBtn.frame=CGRectMake(100,50,36,36);
[qqLoginBtnsetTitle:@"登陸"forState:UIControlStateNormal];
[qqLoginBtnaddTarget:selfaction:@selector(loginAct)forControlEvents:UIControlEventTouchDown];
[self.viewaddSubview:qqLoginBtn];

//2,初始lable
resultLable=[[UILabelalloc]initWithFrame:CGRectMake(30,100,200,36)];
tokenLable=[[UILabelalloc]initWithFrame:CGRectMake(30,150,200,36)];
[self.viewaddSubview:resultLable];
[self.viewaddSubview:tokenLable];

//3,初始化TencentOAuth對(duì)象appid來自應(yīng)用寶創(chuàng)建的應(yīng)用,deletegate設(shè)置為self一定記得實(shí)現(xiàn)代理方法

//這里的appid填寫應(yīng)用寶得到的id記得修改“TARGETS”一欄,在“info”標(biāo)簽欄的“URLtype”添加的“URLscheme”,新的scheme。有問題家QQ群414319235提問
tencentOAuth=[[TencentOAuthalloc]initWithAppId:@"1104617535"andDelegate:self];

//4,設(shè)置需要的權(quán)限列表,此處盡量使用什么取什么。
permissions=[NSArrayarrayWithObjects:@"get_user_info",@"get_simple_userinfo",@"add_t",nilnil];

}
#pragmamark--login
-(void)loginAct
{
NSLog(@"loginAct");
[tencentOAuthauthorize:permissionsinSafari:NO];
}

#pragmamark--TencentSessionDelegate
//登陸完成調(diào)用
-(void)tencentDidLogin
{
resultLable.text=@"登錄完成";

if(tencentOAuth.accessToken&&0!=[tencentOAuth.accessTokenlength])
{
//記錄登錄用戶的OpenID、Token以及過期時(shí)間
tokenLable.text=tencentOAuth.accessToken;
[tencentOAuthgetUserInfo];
}
else
{
tokenLable.text=@"登錄不成功沒有獲取accesstoken";
}
}

//非網(wǎng)絡(luò)錯(cuò)誤導(dǎo)致登錄失敗:
-(void)tencentDidNotLogin:(BOOL)cancelled
{
NSLog(@"tencentDidNotLogin");
if(cancelled)
{
resultLable.text=@"用戶取消登錄";
}else{
resultLable.text=@"登錄失敗";
}
}
//網(wǎng)絡(luò)錯(cuò)誤導(dǎo)致登錄失敗:
-(void)tencentDidNotNetWork
{
NSLog(@"tencentDidNotNetWork");
resultLable.text=@"無網(wǎng)絡(luò)連接,請(qǐng)?jiān)O(shè)置網(wǎng)絡(luò)";
}

-(void)didReceiveMemoryWarning{
[superdidReceiveMemoryWarning];
//Disposeofanyresourcesthatcanberecreated.
}

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

@end


九,真機(jī)測(cè)試效果

打開登陸界面:

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

登陸中

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

登錄成功

將來的自己,會(huì)感謝現(xiàn)在不放棄的自己!

總結(jié)

以上是生活随笔為你收集整理的第三方登录之qq登录(转载)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。