在開發(fā)過程中,常常會遇到第三方授權登錄,這里用友盟比較快捷方便
1.導入友盟的SDK,當然用CocoaPods更加方便快捷
pod 'UMengSocial'
2.設置友盟appkey
- 獲取友盟Appkey漾根。如果你之前已經在友盟注冊了應用祥得,獲得了Appkey婆排,可以繼續(xù)使用之前獲得Appkey。
- 如果你尚未在友盟注冊賬號傲隶,需要先注冊,注冊之后登錄你的賬號,點擊添加新應用,完成新應用填寫之后炭序,將進入應用管理頁面。在該頁面就能得到Appkey.
- 在代碼中設置你的友盟Appkey苍日,在AppDelegate
文件內設置你的AppKey:
#import "UMSocial.h"
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
[UMSocialData setAppKey:@"507fcab25270157b37000010"];
}
3.微信登錄
- 使用微信分享首先需要在微信開放平臺(點擊鏈接)申請惭聂,按照要求填寫應用信息,審核通過后獲取到微信APPID和APPsecret
在你的程序APPdelegate入口方法添加下面的代碼
#import "UMSocialWechatHandler.h"
//設置微信AppId相恃、appSecret辜纲,分享url
[UMSocialWechatHandler setWXAppId:@"wxd930ea5d5a258f4f"
appSecret:@"db426a9829e4b49a0dcac7b4162da6b6"
url:@"http://www.umeng.com/social"];
- 在微信登錄按鈕中實現(xiàn)下面的方法
UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToWechatSession];
snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
if (response.responseCode == UMSResponseCodeSuccess) {
UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary]valueForKey:UMShareToWechatSession];
NSLog(@"username is %@, uid is %@, token is %@ url is %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL);
}
});
-
當然還要去Info.plist中設置 URL schemes
在你的工程設置項,targets 一欄下,選中自己的 target,在 Info->URL Types 中添加 URL Schemes。如果使用的是Xcode3或更低版本拦耐,則需要在plist文件中添加耕腾。獲取各個平臺appkey或者appid的方法可以參考分享詳細文檔綁定各個平臺部分,各個平臺的url scheme格式如下表:
屏幕快照 2016-04-15 上午11.44.49.png - 之后還要去設置白名單
<key>LSApplicationQueriesSchemes</key>
<array>
<string>iosamap</string>
<string>alipay</string>
<string>alipayshare</string>
<string>wechat</string>
<string>weixin</string>
<string>sinaweibohd</string>
<string>sinaweibo</string>
<string>sinaweibosso</string>
<string>weibosdk</string>
<string>weibosdk2.5</string>
<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>wtloginmqq</string>
<string>wtloginmqq2</string>
<string>mqqwpa</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>mqqbrowser</string>
<string>mttbrowser</string>
</array>
這個就是微信的登錄方法,在登錄后就會回調回你所需要的信息.
4.微博登錄
- 在你的工程設置項,targets 一欄下,選中自己的 target,在 Info->URL Types 中添加 URL Schemes,格式為“wb”+新浪appkey杀糯,例如“wb126663232”
- 配置微博APPkey
#import "UMSocialSinaSSOHandler.h"
//第一個參數為新浪appkey,第二個參數為新浪secret扫俺,第三個參數是新浪微博回調地址,這里必須要和你在新浪微博后臺設置的回調地址一致固翰。
[UMSocialSinaSSOHandler openNewSinaSSOWithAppKey:@"3921700954"
secret:@"04b48b094faeb16683c32669824ebdad"
RedirectURL:@"http://sns.whalecloud.com/sina2/callback"];
/**
這里處理新浪微博SSO授權之后跳轉回來狼纬,和微信分享完成之后跳轉回來
*/
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
return [UMSocialSnsService handleOpenURL:url wxApiDelegate:nil];
}
/**
這里處理新浪微博SSO授權進入新浪微博客戶端后進入后臺,再返回原來應用
*/
- (void)applicationDidBecomeActive:(UIApplication *)application
{
[UMSocialSnsService applicationDidBecomeActive];
}
- 刪除授權調用下面的方法
[[UMSocialDataService defaultDataService]
requestUnOauthWithType:UMShareToSina completion:^(UMSocialResponseEntity *response){
NSLog(@"response is %@",response);
}];
4.QQ及Qzone登錄
QQ登錄只支持SSO登錄方式骂际,必須具備手機QQ客戶端疗琉,Qzone默認調用SSO登錄
添加配置文件參考文檔:添加QQ,添加相關庫文件歉铝,配置URL schemes及添加系統(tǒng)回調
在AppDelegate應用入口方法添加下面的方法
//設置分享到QQ的應用Id盈简,和分享url 鏈接
#import "UMSocialQQHandler.h"
[UMSocialQQHandler setQQWithAppId:@"100424468"
appKey:@"c7394704798a158208a74ab60104f0ba"
url:@"http://www.umeng.com/social"];
在QQ登錄按鈕中實現(xiàn)下面的方法
UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToQQ];
snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
// 獲取微博用戶名、uid太示、token等
if (response.responseCode == UMSResponseCodeSuccess) {
UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:UMShareToQQ];
NSLog(@"username is %@, uid is %@, token is %@ url is %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL);
}});
如果還有哪里不明白,可以直接去友盟官方文檔查看