第三方登录之qq登录(转载)
iOS QQ第三方登實現
我們經常會見到應用登陸的時候會有QQ,微信,微博等的第三方登陸
如圖:
下面我們主要講一下qq的第三方登陸如何實現
首先,到官網注冊:
http://wiki.connect.qq.com
一,下載SDK
下載SDK 地址:
http://wiki.open.qq.com/wiki/mobile/SDK下載
下載最新版本的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,登陸
[tencentOAuthauthorize:permissionsinSafari:NO];
5,在代碼中實現 TencentSessionDelegate 方法
#pragma mark -- TencentSessionDelegate
//登陸完成調用
- (void)tencentDidLogin
{
resultLable.text=@"登錄完成";
if(tencentOAuth.accessToken&&0!= [tencentOAuth.accessTokenlength])
{
// 記錄登錄用戶的OpenID、Token以及過期時間
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.
}
以上方法基本上就實現了登陸,下來我們得考慮登陸成功之后如何獲取用戶信息
其實方法很簡單我們在登陸成功的方法里面調用
[tencentOAuthgetUserInfo];
然后系統會調用一個方法(我們需要提前實現)
-(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,初始化登陸按鈕添加到當前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對象appid來自應用寶創建的應用,deletegate設置為self一定記得實現代理方法
//這里的appid填寫應用寶得到的id記得修改“TARGETS”一欄,在“info”標簽欄的“URLtype”添加的“URLscheme”,新的scheme。有問題家QQ群414319235提問
tencentOAuth=[[TencentOAuthalloc]initWithAppId:@"1104617535"andDelegate:self];
//4,設置需要的權限列表,此處盡量使用什么取什么。
permissions=[NSArrayarrayWithObjects:@"get_user_info",@"get_simple_userinfo",@"add_t",nilnil];
}
#pragmamark--login
-(void)loginAct
{
NSLog(@"loginAct");
[tencentOAuthauthorize:permissionsinSafari:NO];
}
#pragmamark--TencentSessionDelegate
//登陸完成調用
-(void)tencentDidLogin
{
resultLable.text=@"登錄完成";
if(tencentOAuth.accessToken&&0!=[tencentOAuth.accessTokenlength])
{
//記錄登錄用戶的OpenID、Token以及過期時間
tokenLable.text=tencentOAuth.accessToken;
[tencentOAuthgetUserInfo];
}
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];
//Disposeofanyresourcesthatcanberecreated.
}
-(void)getUserInfoResponse:(APIResponse*)response
{
NSLog(@"respons:%@",response.jsonResponse);
}
@end
九,真機測試效果
打開登陸界面:
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
登陸中
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
登錄成功
將來的自己,會感謝現在不放棄的自己!
總結
以上是生活随笔為你收集整理的第三方登录之qq登录(转载)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: vue.js组件(component)
- 下一篇: TCL中环沙特光伏晶体晶片工厂项目签署《