前言
開(kāi)發(fā)環(huán)境:xcode7 +ios9
第三方組件:友盟社會(huì)化分享組件
一.集成微博登錄
1.首先新建一個(gè)工程,適配IOS9
需要在Build Setting中將Enable bitcode關(guān)閉,或設(shè)置編譯標(biāo)識(shí)ENABLE_BITCODE=NO。
在右鍵info.plist用source code打開(kāi)正压,info.plist中復(fù)制粘貼以下鏈接中的代碼
三段代碼參考欣福,http://dev.umeng.com/social/ios/ios9
2.然后打開(kāi)微博開(kāi)放平臺(tái)http://open.weibo.com/,注冊(cè)一個(gè)應(yīng)用焦履。會(huì)獲得一個(gè)App Key和App Secre拓劝。
有幾個(gè)注意點(diǎn):
第一基本信息中的Bundle ID要和工程的Bundle ID一致。
第二高級(jí)信息中的授權(quán)回調(diào)頁(yè)和取消授權(quán)回調(diào)頁(yè)面嘉裤、工程代碼中的回調(diào)頁(yè)面郑临,三者保持一致。直接填寫http://sns.whalecloud.com/sina2/callback
第三測(cè)試信息中屑宠,添加測(cè)試賬號(hào)厢洞,例如你自己的微博昵稱。(此時(shí)你的應(yīng)用還未通過(guò)審核的情況下典奉,只能添加部分測(cè)試賬號(hào)來(lái)獲得微博的授權(quán)進(jìn)行第三方登錄或者分享)
2.友盟官網(wǎng)http://www.umeng.com/注冊(cè)躺翻,
使用它們的其中一個(gè)產(chǎn)品,社會(huì)化分享組件卫玖,來(lái)集成第三方的微博公你,http://mobile.umeng.com/social?spm=0.0.0.0.5o2zs7
點(diǎn)擊立即使用,先創(chuàng)建一個(gè)應(yīng)用假瞬,
對(duì)社交平臺(tái)進(jìn)行配置陕靠,里面有微博的配置。
App Key 和App Secre 就填寫第一步中在微博開(kāi)放平臺(tái)注冊(cè)應(yīng)用所獲得的脱茉。
接下來(lái)大致看下文檔的介紹:http://dev.umeng.com/social/ios/login-page
然后去選擇下載SDKhttp://dev.umeng.com/social/ios/sdk-download剪芥,目前最新的是5.2.1版本的。
邊下載就邊仔細(xì)看文檔的介紹了:http://dev.umeng.com/social/ios/login-page
3.工程代碼
AppDelegate.m文件中
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[UMSocialData setAppKey:@"5743f8cbe0f55ad4e3001729"];//友盟ID
[UMSocialSinaSSOHandler openNewSinaSSOWithAppKey:@"3914982255"http://sina ID
secret:@"???" //sina secret
RedirectURL:@"http://sns.whalecloud.com/sina2/callback"];
[UMSocialData openLog:YES];
return YES;
}
實(shí)現(xiàn)微博授權(quán)成功跳回之前的應(yīng)用
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
NSLog(@"返回的地址%@",url);//結(jié)果是wb+sina應(yīng)用ID開(kāi)頭的字符串
return [UMSocialSnsService handleOpenURL:url wxApiDelegate:nil];
}
ViewController.m文件中
在新浪微博登錄按鈕中實(shí)現(xiàn)下面的方法
UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToSina]; snsPlatform.loginClickHandler(self,[UMSocialControllerService
defaultControllerService],YES,^(UMSocialResponseEntity *response){// 獲取微博用戶名芦劣、uid、token等 if (response.responseCode == UMSResponseCodeSuccess) { NSDictionary *dict = [UMSocialAccountManager socialAccountDictionary]; UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:snsPlatform.platformName]; NSLog(@"\nusername = %@,\n usid = %@,\n token = %@ iconUrl = %@,\n unionId = %@,\n thirdPlatformUserProfile = %@,\n thirdPlatformResponse = %@ \n, message = %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL, snsAccount.unionId, response.thirdPlatformUserProfile, response.thirdPlatformResponse, response.message); }});
二说榆、集成QQ登錄
1.導(dǎo)入友盟實(shí)例工程中的QQ的SDK到自己的工程虚吟,修改路徑,不然會(huì)報(bào)找不到頭文件的錯(cuò)誤
工程目錄如下:
2.在AppDelegate應(yīng)用入口方法添加下面的方法
#import "UMSocialQQHandler.h"
//友盟默認(rèn)的id和appkey,直接復(fù)制的
[UMSocialQQHandler setQQWithAppId:@"100424468" appKey:@"c7394704798a158208a74ab60104f0ba" url:@"http://www.umeng.com/social"];
3.在QQ登錄按鈕中復(fù)制下面友盟的方法
UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToQQ];
snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
// 獲取微博用戶名签财、uid串慰、token等
if (response.responseCode == UMSResponseCodeSuccess) {
NSDictionary *dict = [UMSocialAccountManager socialAccountDictionary];
UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:snsPlatform.platformName];
NSLog(@"\nusername = %@,\n usid = %@,\n token = %@ iconUrl = %@,\n unionId = %@,\n thirdPlatformUserProfile = %@,\n thirdPlatformResponse = %@ \n, message = %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL, snsAccount.unionId, response.thirdPlatformUserProfile, response.thirdPlatformResponse, response.message);
}});
4.添加URL
模擬器也可以運(yùn)行,直接網(wǎng)頁(yè)登錄QQ唱蒸,有裝客戶端就會(huì)調(diào)用客戶端邦鲫。
5.結(jié)果截圖
三、微信登錄
1.導(dǎo)入SDK,運(yùn)行報(bào)錯(cuò)找不到.a文件庆捺,修改路徑即可
2.添加代碼
//導(dǎo)入頭文件
#import "UMSocialWechatHandler.h"
在AppDelegate應(yīng)用入口方法添加下面的方法
//友盟默認(rèn)的微信AppId古今、appSecret,分享url
[UMSocialWechatHandler setWXAppId:@"wxd930ea5d5a258f4f" appSecret:@"db426a9829e4b49a0dcac7b4162da6b6" url:@"http://www.umeng.com/social"];
3.微信登錄按鈕中實(shí)現(xiàn)
- (IBAction)loginWX:(id)sender {
UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToWechatSession];
snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
if (response.responseCode == UMSResponseCodeSuccess) {
NSDictionary *dict = [UMSocialAccountManager socialAccountDictionary];
UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:snsPlatform.platformName];
NSLog(@"\nusername = %@,\n usid = %@,\n token = %@ iconUrl = %@,\n unionId = %@,\n thirdPlatformUserProfile = %@,\n thirdPlatformResponse = %@ \n, message = %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL, snsAccount.unionId, response.thirdPlatformUserProfile, response.thirdPlatformResponse, response.message);
}
});
}
4.添加URL
wxd930ea5d5a258f4f