Untitled.gif
友盟分享
-
首先注冊友盟賬號 SDK下載
Snip20160616_2.png - 友盟個人中心中創建一個應用程序
Snip20160616_3.png
- 新建項目 將友盟的庫拖入工程中
Snip20160616_6.png
- 配置項目環境跟添加需要的第三方庫
Snip20160616_7.png
- 在 AppDelegate文件內設置你的AppKey:
#import "UMSocial.h"
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[UMSocialData setAppKey:@"507fcab25270157b37000010"];
}
- 在你的控制器View添加分享按鈕 關聯Action
Snip20160616_8.png
- 在分享的方法里實現代碼
#import "UMSocial.h"
[UMSocialData defaultData].extConfig.title = @"友盟分享測試";
// 這里設置是點擊微信分享的內容進入的URL
[UMSocialData defaultData].extConfig.wechatSessionData.url = @"http://www.chemayi.com";
/**
* self代表在哪個控制器彈出默認的分享界面
*/
[UMSocialSnsService presentSnsIconSheetView:self // 在哪個控制器的View顯示分享
appKey:@"5762b194e0f55a8a740001a5"
shareText:@"人生要有要有夢想"
shareImage:[UIImage imageNamed:@"1"]
shareToSnsNames:@[UMShareToWechatSession,UMShareToWechatTimeline,UMShareToSina,UMShareToQQ,UMShareToQzone, UMShareToEmail,UMShareToQQ, UMShareToFacebook, UMShareToYXSession, UMShareToLWSession, UMShareToAlipaySession, UMShareToDouban]
delegate:nil];
}
Snip20160616_9.png
注意: 默認情況下微信跟朋友圈一些分享圖標是不會顯示出來的
通常點擊彈出對應的分享的icon后,進入時一個webView,也就是需要OAuth2.0授權,需要用戶輸入賬戶名跟密碼進行授權,但是目前一般App很少直接讓用戶輸入密碼跟賬號,都是采取SSO授權
SSO授權
本質還是OAuth2.0 例如我們微信分享,當用戶手機上已將安裝微信客戶端以后,點擊微信分享后,自動通過已登錄的微信賬號進行授權分享
-
SSO授權通常配置的步驟(額外需要添加的步驟)
- 拿新浪微博舉例
解壓下載文件夾,將SinaSSO文件夾添加到工程。
Snip20160616_10.png
在other linker flags增加-ObjC 選項,并添加ImageIO 系統framework
-
配置URL schemes
- 在你的工程設置項,targets 一欄下,選中自己的 target,在 Info->URL Types 中添加 URL Schemes,格式為“wb”+新浪appkey,例如“wb126663232”
Snip20160616_11.png
- 配置微博APPkey
#import "UMSocialSinaSSOHandler.h"
//第一個參數為新浪appkey,第二個參數為新浪secret,第三個參數是新浪微博回調地址,這里必須要和你在新浪微博后臺設置的回調地址一致。
[UMSocialSinaSSOHandler openNewSinaSSOWithAppKey:@"3921700954"
secret:@"04b48b094faeb16683c32669824ebdad"
RedirectURL:@"http://sns.whalecloud.com/sina2/callback"];
注意: 回調URL必須和后臺保持一致,否則會返回錯誤碼21322
- 添加系統回調
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
BOOL result = [UMSocialSnsService handleOpenURL:url];
if (result == FALSE) {
//調用其他SDK,例如支付寶SDK等
}
return result;
}
- 這樣配置以后,我們在點擊分享就直接進入新浪客戶端
微信分享
- 注冊微信平臺賬號(需要審核)
- 管理賬號
Snip20160616_13.png
- 點擊查看
Snip20160616_14.png
- 添加相關文件
Snip20160617_15.png
- 添加SDK依賴的系統庫文件。(如果已經添加過就不需要添加了)
Snip20160617_16.png
- 在新發布的iOS9系統上圍繞用戶數據的安全性和體驗新增了一些安全特性,同時也影響了應用的實現以及集成方式,為了保證良好的穩定性和體驗,需要做如下處理:
- 以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連接都可正常請求
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
- 應用跳轉(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>alipay</string>
<string>alipayshare</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>
注:以上部分平臺官方未給出相應白名單,由技術人員測試各個平臺所收集而來,如果有所遺漏,請自行加入并向客服說明,我們會進一步補充名單。
- 應用瘦身(App Thining)
iOS9 SDK新增了對App瘦身的功能,詳情見App Thining。目前各個第三方平臺正在進行App thining的支持,所以為了正常使用第三方SDK及分享SDK,需要在Build Setting中將Enable bitcode關閉,或設置編譯標識ENABLE_BITCODE=NO。
注:bitcode僅在Xcode7以上顯示并默認開啟。
Snip20160617_19.png
- 在你的程序APPdelegate入口方法添加下面的代碼
//設置微信AppId、appSecret,分享url
[UMSocialWechatHandler setWXAppId:@"wxe030296d5c5968b4" appSecret:@"0cde569ce201db3701b558fd2405ec0b" url:@"http://www.hao123.com"];
return YES;
- 注意:如果不添加上面的代碼,則分享列表中不會出現微信及朋友圈圖標
- URL必須為http鏈接
Snip20160617_17.png
- 配置URL schemes
Snip20160616_11.png
- 添加系統回調
- 在APPdelegate中實現下面兩個系統回調方法:
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
BOOL result = [UMSocialSnsService handleOpenURL:url];
if (result == FALSE) {
//調用其他SDK,例如支付寶SDK等
}
return result;
}
- ViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
}
- (IBAction)share:(id)sender {
[UMSocialData defaultData].extConfig.title = @"友盟分享測試";
[UMSocialData defaultData].extConfig.wechatSessionData.url = @"http://www.chemayi.com";
/**
* self代表在哪個控制器彈出默認的分享界面
*/
[UMSocialSnsService presentSnsIconSheetView:self // 在哪個控制器的View顯示分享
appKey:@"5762b194e0f55a8a740001a5"
shareText:@"人生要有要有夢想"
shareImage:[UIImage imageNamed:@"1"]
shareToSnsNames:@[UMShareToWechatSession,UMShareToWechatTimeline,UMShareToSina,UMShareToQQ,UMShareToQzone, UMShareToEmail,UMShareToQQ, UMShareToFacebook, UMShareToYXSession, UMShareToLWSession, UMShareToAlipaySession, UMShareToDouban]
delegate:nil];
}
@end
Untitled.gif