前言
開發環境:xcode7 +ios9
第三方組件:友盟社會化分享組件
一.集成微博登錄
1.首先新建一個工程,適配IOS9
需要在Build Setting中將Enable bitcode關閉,或設置編譯標識ENABLE_BITCODE=NO。
在右鍵info.plist用source code打開,info.plist中復制粘貼以下鏈接中的代碼
三段代碼參考,http://dev.umeng.com/social/ios/ios9
2.然后打開微博開放平臺http://open.weibo.com/,注冊一個應用。會獲得一個App Key和App Secre。
有幾個注意點:
第一基本信息中的Bundle ID要和工程的Bundle ID一致。
第二高級信息中的授權回調頁和取消授權回調頁面、工程代碼中的回調頁面,三者保持一致。直接填寫http://sns.whalecloud.com/sina2/callback
第三測試信息中,添加測試賬號,例如你自己的微博昵稱。(此時你的應用還未通過審核的情況下,只能添加部分測試賬號來獲得微博的授權進行第三方登錄或者分享)
2.友盟官網http://www.umeng.com/注冊,
使用它們的其中一個產品,社會化分享組件,來集成第三方的微博,http://mobile.umeng.com/social?spm=0.0.0.0.5o2zs7
點擊立即使用,先創建一個應用,
對社交平臺進行配置,里面有微博的配置。
App Key 和App Secre 就填寫第一步中在微博開放平臺注冊應用所獲得的。
接下來大致看下文檔的介紹:http://dev.umeng.com/social/ios/login-page
然后去選擇下載SDKhttp://dev.umeng.com/social/ios/sdk-download,目前最新的是5.2.1版本的。
邊下載就邊仔細看文檔的介紹了: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;
}
實現微博授權成功跳回之前的應用
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
NSLog(@"返回的地址%@",url);//結果是wb+sina應用ID開頭的字符串
return [UMSocialSnsService handleOpenURL:url wxApiDelegate:nil];
}
ViewController.m文件中
在新浪微博登錄按鈕中實現下面的方法
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.導入友盟實例工程中的QQ的SDK到自己的工程,修改路徑,不然會報找不到頭文件的錯誤
工程目錄如下:
2.在AppDelegate應用入口方法添加下面的方法
#import "UMSocialQQHandler.h"
//友盟默認的id和appkey,直接復制的
[UMSocialQQHandler setQQWithAppId:@"100424468" appKey:@"c7394704798a158208a74ab60104f0ba" url:@"http://www.umeng.com/social"];
3.在QQ登錄按鈕中復制下面友盟的方法
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
模擬器也可以運行,直接網頁登錄QQ,有裝客戶端就會調用客戶端。
5.結果截圖
三、微信登錄
1.導入SDK,運行報錯找不到.a文件,修改路徑即可
2.添加代碼
//導入頭文件
#import "UMSocialWechatHandler.h"
在AppDelegate應用入口方法添加下面的方法
//友盟默認的微信AppId、appSecret,分享url
[UMSocialWechatHandler setWXAppId:@"wxd930ea5d5a258f4f" appSecret:@"db426a9829e4b49a0dcac7b4162da6b6" url:@"http://www.umeng.com/social"];
3.微信登錄按鈕中實現
- (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