官方網址: http://dev.umeng.com/social/ios/operation#2
一、注冊友盟賬號
登陸友盟官網,在我的產品頁面添加新應用,然后獲取到Appkey.
二、申請第三方賬號
1.新浪微博
登錄新浪微博開放平臺(點擊鏈接),填寫相關應用信息并上傳icon圖片。注意修改安全域名為sns.whalecloud.com 同時設置授權回調頁為http://sns.whalecloud.com/sina2/callback 安全域名設置在應用信息-->基本信息.
授權回調頁、取消授權回調頁設置在應用信息-->高級信息.
- 安全域名的修改需要二次審核通過才生效,授權回調頁修改即時生效.
2.微信
登錄微信開放平臺(點擊鏈接),填寫相關應用信息,審核通過后獲取到微信AppID及AppSecret,如果需要微信登錄功能,需要申請微信登錄權限.
3.QQ及Qzone
QQ及Qzone使用同一個AppID及Appkey,登錄騰訊開放平臺(點擊鏈接) ,選擇移動應用,填寫相關應用信息并提交審核,未審核前通過只能使用測試賬號,添加測試賬號方法如下: 選擇用戶能力-->進階社交能力-->應用調試者,添加測試賬號必須在申請者好友列表中.
4.人人網
登錄人人開放平臺(點擊鏈接) ,填寫相關應用信息,同時填寫應用根域名為sns.whalecloud.com 具體位置: 基本信息-->應用根域名.
5.豆瓣
登錄豆瓣開放平臺(點擊鏈接),創建應用并填寫相關應用信息,注意權限必須選擇廣播,同時填寫回調地址為http://sns.whalecloud.com/douban/callback.
三、綁定第三方賬號到友盟后臺
目前需要在友盟后臺綁定的第三方賬號為:新浪微博、騰訊微博、人人網、豆瓣、Qzone,其余平臺如微信、QQ直接在代碼中設置.
綁定地址:http://umeng.com/apps
登錄友盟網站->左上角選擇你們的產品->組件->社會化組件->設置.
1.短鏈接開關
短鏈接開關只對新浪微博、騰訊微博、人人網、豆瓣四個平臺有效,開啟短鏈接開關,分享文案中附加的鏈接會被轉碼,同時可以統計到分享回流率(點擊鏈接的次數),關閉短鏈接開關則無法統計,短鏈接開關默認為關閉狀態.
2.文字截斷開關
文字截斷開關只對新浪微博、騰訊微博、人人網、豆瓣四個平臺有效,同時只對使用自定義分享編輯頁或沒有分享編輯頁用戶有效,當分享文案超出字數限制時自動截斷,開關狀態默認關閉。
四、適配iOS9系統
在新發布的iOS9系統上圍繞用戶數據的安全性和體驗新增了一些安全特性,同時也影響了應用的實現以及集成方式,為了保證良好的穩定性和體驗,需要做如下處理:
1.HTTP傳輸安全
以iOS9 SDK編譯的工程會默認以SSL安全協議進行網絡傳輸,即HTTPS,如果依然使用HTTP協議請求網絡會報系統異常并中斷請求。目前可用如下兩種方式保持用HTTP進行網絡連接:
A、在info.plist中加入安全域名白名單(右鍵info.plist用source code打開)
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>log.umsns.com</key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSTemporaryExceptionMinimumTLSVersion</key>
<string>TLSv1.1</string>
</dict>
<key>sns.whalecloud.com</key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSTemporaryExceptionMinimumTLSVersion</key>
<string>TLSv1.1</string>
</dict>
<!-- 集成新浪微博對應的HTTP白名單-->
<key>sina.cn</key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
<false/>
</dict>
<key>weibo.cn</key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
<false/>
</dict>
<key>weibo.com</key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSThirdPartyExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
<false/>
</dict>
<key>sinaimg.cn</key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSThirdPartyExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
<false/>
</dict>
<key>sinajs.cn</key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSThirdPartyExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
<false/>
</dict>
<key>sina.com.cn</key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSThirdPartyExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
<false/>
</dict>
<!-- 新浪微博-->
<!-- 集成微信、QQ、Qzone、騰訊微博授權對應的HTTP白名單-->
<key>qq.com</key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSThirdPartyExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
<false/>
</dict>
<!-- 騰訊授權-->
<!-- 集成人人授權對應的HTTP白名單-->
<key>renren.com</key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSThirdPartyExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
<false/>
</dict>
<!-- 人人授權-->
<!-- 集成Facebook授權對應的HTTP白名單-->
<key>facebook.com</key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSExceptionRequiresForwardSecrecy</key>
<false/>
</dict>
<key>fbcdn.net</key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSExceptionRequiresForwardSecrecy</key>
<false/>
</dict>
<key>akamaihd.net</key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSExceptionRequiresForwardSecrecy</key>
<false/>
</dict>
<!-- Facebook授權-->
<!-- 集成Twitter授權對應的HTTP白名單-->
<key>twitter.com</key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSExceptionRequiresForwardSecrecy</key>
<false/>
</dict>
<!-- Twitter授權-->
</dict>
</dict>
注:以上部分平臺官方未給出相應白名單,由技術人員測試各個平臺所收集而來,如果有所遺漏,請自行加入并向客服說明,我們會進一步補充名單.
B、在info.plist的NSAppTransportSecurity下新增NSAllowsArbitraryLoads并設置為YES,指定所有HTTP連接都可正常請求 http://dev.umeng.com/social/ios/ios9
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
2.應用跳轉(SSO等)
如果你的應用使用了如SSO授權登錄或跳轉分享功能,在iOS9下就需要增加一個可跳轉的白名單,指定對應跳轉App的URL Scheme,否則將在第三方平臺判斷是否跳轉時用到的canOpenURL時返回NO,進而只進行webview授權或授權/分享失敗.
同樣在info.plist增加:
<key>LSApplicationQueriesSchemes</key>
<array>
<!-- 微信 URL Scheme 白名單-->
<string>wechat</string>
<string>weixin</string>
<!-- 新浪微博 URL Scheme 白名單-->
<string>sinaweibohd</string>
<string>sinaweibo</string>
<string>sinaweibosso</string>
<string>weibosdk</string>
<string>weibosdk2.5</string>
<!-- QQ、Qzone URL Scheme 白名單-->
<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>
<!-- 人人 URL Scheme 白名單-->
<string>renrenios</string>
<string>renrenapi</string>
<string>renren</string>
<string>renreniphone</string>
<!-- 來往 URL Scheme 白名單-->
<string>laiwangsso</string>
<!-- 易信 URL Scheme 白名單-->
<string>yixin</string>
<string>yixinopenapi</string>
<!-- instagram URL Scheme 白名單-->
<string>instagram</string>
<!-- whatsapp URL Scheme 白名單-->
<string>whatsapp</string>
<!-- line URL Scheme 白名單-->
<string>line</string>
<!-- Facebook URL Scheme 白名單-->
<string>fbapi</string>
<string>fb-messenger-api</string>
<string>fbauth2</string>
<string>fbshareextension</string>
</array>
- 注:以上部分平臺官方未給出相應白名單,由技術人員測試各個平臺所收集而來,如果有所遺漏,請自行加入并向客服說明,我們會進一步補充名單。
3. 應用瘦身(App Thining)
iOS9 SDK新增了對App瘦身的功能,詳情見App Thining。目前各個第三方平臺正在進行App thining的支持,所以為了正常使用第三方SDK及分享SDK,需要在Build Setting中將Enable bitcode關閉,或設置編譯標識ENABLE_BITCODE=NO。
- 注:bitcode僅在Xcode7以上顯示并默認開啟。
五、下載并安裝SDK
1.使用cocoaPods
如需使用CocoaPods 在Podfile中增加一行,內容如下:
pod 'UMengSocial', '~> 5.0'
然后執行安裝即可
pod install
2.下載SDK
http://dev.umeng.com/social/ios/sdk-download
3.設置友盟apply
- 獲取友盟Appkey。如果你之前已經在友盟注冊了應用,獲得了Appkey,可以繼續使用之前獲得Appkey.
- 如果你尚未在友盟注冊賬號,需要先注冊,注冊之后登錄你的賬號,點擊添加新應用,完成新應用填寫之后,將進入應用管理頁面。在該頁面就能得到Appkey.
- 在代碼中設置你的友盟Appkey,在 AppDelegate文件內設置你的AppKey:
#import "UMSocial.h"
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[UMSocialData setAppKey:@"your Umeng appKey"];
// 用下面的代碼打開我們SDK在控制臺的輸出后能看到相應的錯誤碼
[UMSocialData openLog:YES];
}
并添加下方的系統回調方法:
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary*)options{
return [UMSocialSnsService handleOpenURL:url];
}
六、配置URL schemes
1.為什么要配置 URL schemes
URL schemes 和跳出應用后返回應用的邏輯相關,因此如果使用了SSO授權功能或QQ、微信、webview方式的Facebook,就必須配置URL schemes,否則跳出應用后將無法返回.
2. 如何設置 URL schemes
在你的工程設置項,targets 一欄下,選中自己的 target,在 Info->URL Types 中添加 URL Schemes。如果使用的是Xcode3或更低版本,則需要在plist文件中添加。獲取各個平臺appkey或者appid的方法可以參考分享詳細文檔綁定各個平臺部分,各個平臺的url scheme格式如下表:
平臺 | url scheme設置格式 ||
------------------|----------------|
新浪微博 | “wb”+新浪appkey,例如“wb126663232” |
微信 | 微信應用appId,例如“wxd9a39c7122aa6516” |
人人網 | “rm”+你的應用bundle id,例如“rmcom.umeng.SocialSDK” |
QQ、QQ空間 | 需要添加兩個URL schemes 1. “QQ”+騰訊QQ互聯應用appId轉換成十六進制(不足8位前面補0),例如“QQ05FC5B14”。生成十六進制方法:點擊鏈接。2.“tencent“+騰訊QQ互聯應用Id,例如“tencent100424468" |
來往 | Identifier填“Laiwang”,URL Schemes填來往AppId.注意使用來往SDK后,Xcode工程other linker flags需要添加-ObjC參數? |
易信 | 易信Appkey,例如“yx35664bdff4db42c2b7be1e29390c1a06” |
Facebook | 集成最新的Facebook SDK在iOS7.0以上有效,若要使用我們提供的facebook分享需要設置“fb”+facebook AppID,例如“fb1440390216179601” |
七、第三方登錄
1.第三方登錄概述
第三方登錄主要用于簡化用戶登錄流程,通過用戶擁有的微博、QQ、微信等第三方賬號進行登錄并且構建APP自己的登錄賬號體系.
實現第三方登錄主要通過下面兩步:
- 在第三方平臺完成授權
- 獲取第三方平臺的accesstoken信息及用戶資料
1.1第三方登錄支持的平臺
目前友盟社會化組件支持的第三方登錄平臺為:新浪微博、微信、QQ、QQ空間、騰訊微博、人人網、豆瓣、Facebook、Twitter.
2.新浪微博登錄
在AppDelegate應用入口方法添加下面的方法:
//設置分享到QQ/Qzone的應用Id,和分享url 鏈接
#import "UMSocialQQHandler.h"
[UMSocialSinaSSOHandler openNewSinaSSOWithAppKey:@"新浪 appkey"
secret:@"新浪secret"
RedirectURL:@"http://sns.whalecloud.com/sina2/callback"];
在新浪微博登錄按鈕中實現下面的方法:
UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToSina];
snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
// 獲取微博用戶名、uid、token等
if (response.responseCode == UMSResponseCodeSuccess) {
UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:UMShareToSina];
NSLog(@"username is %@, uid is %@, token is %@ url is %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL);
}});
刪除授權調用下面的方法:
[[UMSocialDataService defaultDataService] requestUnOauthWithType:UMShareToSina completion:^(UMSocialResponseEntity *response){
NSLog(@"response is %@",response);
}];
3.QQ及Qzone登錄
QQ登錄只支持SSO登錄方式,必須具備手機QQ客戶端,Qzone默認調用SSO登錄.
添加配置文件參考文檔:添加QQ及Qzone,添加相關庫文件,配置URL schemes及添加系統回調,在AppDelegate應用入口方法添加下面的方法:
//設置分享到QQ/Qzone的應用Id,和分享url 鏈接
#import "UMSocialQQHandler.h"
[UMSocialQQHandler setQQWithAppId:@"qq id" appKey:@"qq apply" url:@"http://www.umeng.com/social"];
在QQ登錄按鈕中實現下面的方法:
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);
}});
在Qzone登錄按鈕中實現下面的方法:
UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToQzone];
snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
// 獲取微博用戶名、uid、token等
if (response.responseCode == UMSResponseCodeSuccess) {
UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:UMShareToQzone];
NSLog(@"username is %@, uid is %@, token is %@ url is %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL);
}});
- 在SDK5.0及之后的版本中,用戶信息都直接在授權之后便會返回,可以在snsAccount里獲取全部的用戶信息資料.
- Qzone將UMShareToQQ替換為UMShareToQzone即可.
4.微信登錄
添加配置文件參考文檔:添加微信及朋友圈,添加相關庫文件,配置URL schemes及添加系統回調:
- 注意微信登錄必須先在微信開放平臺申請微信登錄權限
在你的程序APPdelegate入口方法添加下面的代碼:
#import "UMSocialWechatHandler.h"
//設置微信AppId、appSecret,分享url
[UMSocialWechatHandler setWXAppId:@"wxd930ea5d5a258f4f" appSecret:@"db426a9829e4b49a0dcac7b4162da6b6" url:@"http://www.umeng.com/social"];
在微信登錄按鈕中實現下面的方法:
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);
}
});
- 在SDK5.0及之后的版本中,用戶信息都直接在授權之后便會返回,可以在snsAccount里獲取全部的用戶信息資料.
八、關于提交項目進行蘋果審核
使用微信分享、登錄必須安裝微信客戶端,QQ登錄、QQ空間分享過程中必須安裝手機QQ客戶端,在未安裝客戶端的設備上測試會提示下載,這是不符合蘋果審核規則的。這是微信及騰訊QQ互聯導致的問題。 解決方案: 首先調用微信SDK或QQ互聯SDK的方法檢測是否安裝微信/QQ客戶端:
[QQApiInterface isQQInstalled];
如果未安裝客戶端則隱藏對應登錄或分享按鈕。