之前適配iOS9 是友盟分享5.0.1版本,請(qǐng)移步:友盟分享5.0.1
更新使用的友盟分享6.0.3版本至目前最新版本6.0.3來適配 iOS10!這里以微信和 QQ 分享舉栗子??~

第一步: 下載最新的SDK
下載SDK鏈接,http://dev.umeng.com/social/ios/sdk-download
第二步:下載目錄簡(jiǎn)介
- Document - U-Share SDK文檔
- UMSocialDemo - U-Share SDK Demo(如點(diǎn)選下載)
- UMSocial - U-Share SDK核心目錄
UMSocialSDK - U-Share SDK核心framework
UMSocialUI - U-Share 分享UI資源、分享面板framework
SocialLibraries - 所選擇下載的第三方平臺(tái)SDK及U-Share鏈接庫(kù)
UMSocialSDKPlugin - SDK需要的依賴插件
第三步:加入下載解壓后的SDK
3.1 添加到工程所需

3.2 添加項(xiàng)目配置
在Other Linker Flags加入-ObjC

3.3 加入依賴系統(tǒng)庫(kù)

3.4 加入以下系統(tǒng)庫(kù)
libsqlite3.tbd
CoreGraphics.framework
3.5 添加平臺(tái)相應(yīng)的依賴庫(kù)
根據(jù)集成的不同平臺(tái)加入相關(guān)的依賴庫(kù),未列出平臺(tái)則不用添加 添加方式:選中項(xiàng)目Target -> Linked Frameworks and Libraries列表中添加, 目前有精簡(jiǎn)版,有些可不用添加,具體參考官方文檔.
微信(完整版)-精簡(jiǎn)版無需添加以下依賴庫(kù) :
SystemConfiguration.framework
CoreTelephony.framework
libsqlite3.tbd
libc++.tbd
libz.tbd
QQ/QZone/TIM(完整版)-精簡(jiǎn)版無需添加以下依賴庫(kù):
SystemConfiguration.framework
libc++.tbd
第四步: 配置各平臺(tái)URL Scheme
4.1 添加URL Types
URL Scheme是通過系統(tǒng)找到并跳轉(zhuǎn)對(duì)應(yīng)app的一類設(shè)置,通過向項(xiàng)目中的info.plist文件中加入U(xiǎn)RL types可使用第三方平臺(tái)所注冊(cè)的appkey信息向系統(tǒng)注冊(cè)你的app,當(dāng)跳轉(zhuǎn)到第三方應(yīng)用授權(quán)或分享后,可直接跳轉(zhuǎn)回你的app。
添加URL Types,以下三種都可進(jìn)行設(shè)置:



我一般習(xí)慣都是選擇第一種通過工程設(shè)置面板.
4.2 配置第三方平臺(tái)URL Scheme
- QQ需增加兩個(gè)URL Scheme:
- "tencent"+騰訊QQ互聯(lián)應(yīng)用appID
“QQ”+騰訊QQ互聯(lián)應(yīng)用appID轉(zhuǎn)換成十六進(jìn)制(不足8位前面補(bǔ)0)
URL Scheme舉例 如appID為:100424468 - tencent100424468
QQ05fc5b14
說明:100424468轉(zhuǎn)十六進(jìn)制為5fc5b14,因不足8位向前補(bǔ)0,結(jié)果為05fc5b14,加"QQ"前綴QQ05fc5b14 - 微信僅需一個(gè)URL Scheme:
直接設(shè)置微信appkey即可,如wxdc1e388c3822c80b
這里的用友盟測(cè)試賬號(hào)測(cè)試,請(qǐng)勿直接使用上述示例參數(shù),多個(gè)App使用同樣的URL Schemes參數(shù)會(huì)導(dǎo)致從第三方App跳轉(zhuǎn)回原App時(shí)混亂!

第五步:適配iOS9/10系統(tǒng)
iOS9系統(tǒng)后Apple對(duì)HTTP請(qǐng)求及訪問外部應(yīng)用做了更加嚴(yán)格的要求,包括HTTP白名單、跳轉(zhuǎn)第三方應(yīng)用白名單等,具體設(shè)置第三方平臺(tái)參數(shù)請(qǐng)參照適配iOS9/10系統(tǒng)。
http://dev.umeng.com/social/ios/ios9
在新發(fā)布的iOS10系統(tǒng)上圍繞用戶數(shù)據(jù)的安全性和體驗(yàn)新增了一些安全特性,同時(shí)也影響了應(yīng)用的實(shí)現(xiàn)以及集成方式,為了保證良好的穩(wěn)定性和體驗(yàn),需要做如下處理:
5.1. HTTPS傳輸安全
以iOS10 SDK編譯的工程會(huì)默認(rèn)以SSL安全協(xié)議進(jìn)行網(wǎng)絡(luò)傳輸,即HTTPS,如果依然使用HTTP協(xié)議請(qǐng)求網(wǎng)絡(luò)會(huì)報(bào)系統(tǒng)異常并中斷請(qǐng)求。目前可用如下兩種方式保持用HTTP進(jìn)行網(wǎng)絡(luò)連接:
在info.plist中加入安全域名白名單(右鍵info.plist用source code打開):

5.2. 應(yīng)用跳轉(zhuǎn)(SSO等)
如果你的應(yīng)用使用了如SSO授權(quán)登錄或跳轉(zhuǎn)分享功能,在iOS9/10下就需要增加一個(gè)可跳轉(zhuǎn)的白名單,指定對(duì)應(yīng)跳轉(zhuǎn)App的URL Scheme,否則將在第三方平臺(tái)判斷是否跳轉(zhuǎn)時(shí)用到的canOpenURL時(shí)返回NO,進(jìn)而只進(jìn)行webview授權(quán)或授權(quán)/分享失敗。
同樣在info.plist增加:

第六步:調(diào)用U-Share SDK(具體實(shí)現(xiàn))
6.1 初始化U-Share及第三方平臺(tái)
app啟動(dòng)后進(jìn)行U-Share和第三方平臺(tái)的初始化工作
以下代碼是拿微信和QQ平臺(tái)初始化放出,開發(fā)者根據(jù)平臺(tái)需要選取相應(yīng)代碼,并替換為所屬注冊(cè)的appKey和appSecret。 至于其他平臺(tái)舉一反三請(qǐng)看官方文檔.
在AppDelegate.m中設(shè)置如下代碼
將 #import <UMSocialCore/UMSocialCore.h>
改成 #import <UMShare/UMShare.h>
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
[self.window switchRootViewController];
[self.window makeKeyAndVisible];
float version = [[[UIDevice currentDevice] systemVersion] floatValue];
if (version >= 8.0) { // iOS8+ IconBadge需授權(quán)
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
}
// 使用友盟
[self useUMengSocialData];
return YES;
}
#pragma mark - 友盟相關(guān)設(shè)置
- (void)confitUShareSettings
{
/*
* 打開圖片水印
*/
//[UMSocialGlobal shareInstance].isUsingWaterMark = YES;
/*
* 關(guān)閉強(qiáng)制驗(yàn)證https,可允許http圖片分享,但需要在info.plist設(shè)置安全域名
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
*/
//[UMSocialGlobal shareInstance].isUsingHttpsWhenShareContent = NO;
}
- (void)useUMengSocialData {
//打開日志
[[UMSocialManager defaultManager] openLog:YES];
//設(shè)置友盟appkey
[[UMSocialManager defaultManager] setUmSocialAppkey:@"57b432afe0f55a9832001a0a"];
// 獲取友盟social版本號(hào)
XHHLog(@"UMeng social version: %@", [UMSocialGlobal umSocialSDKVersion]);
//各平臺(tái)的詳細(xì)配置
//設(shè)置分享到QQ互聯(lián)的appId和appKey
[[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_QQ appKey:@"100424468" appSecret:nil redirectURL:@"http://mobile.umeng.com/social"];
//設(shè)置微信的appId和appKey
[[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_WechatSession appKey:@"wxdc1e388c3822c80b" appSecret:@"3baf1193c85774b3fd9d18447d76cab0" redirectURL:@"http://mobile.umeng.com/social"];
// 如果不想顯示平臺(tái)下的某些類型(微信收藏),可用以下接口設(shè)置
[[UMSocialManager defaultManager] removePlatformProviderWithPlatformTypes:@[@(UMSocialPlatformType_WechatFavorite)]];
}
6.2 設(shè)置系統(tǒng)回調(diào)
在AppDelegate.m中設(shè)置如下代碼
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
BOOL result = [[UMSocialManager defaultManager] handleOpenURL:url];
if (!result) {
}
return result;
}
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
BOOL result = [[UMSocialManager defaultManager] handleOpenURL:url];
if (!result) {
}
return result;
}
6.3 第三方平臺(tái)分享
在所需控制器里:
由于6.1版開始更新了全新的分享面板,所以導(dǎo)入形式也略有改變. 將 #import <UMSocialCore/UMSocialCore.h> 改成 #import <UMShare/UMShare.h> 將#import "UMSocialUIManager.h" 改成#import <UShareUI/UShareUI.h>
#import <UMShare/UMShare.h>
#import <UShareUI/UShareUI.h>
接口沒變,回調(diào)block參數(shù)調(diào)整將[UMSocialUIManager showShareMenuViewInWindowWithPlatformSelectionBlock:^(UMShareMenuSelectionView *shareSelectionView, UMSocialPlatformType platformType){ }];改為[UMSocialUIManager showShareMenuViewInWindowWithPlatformSelectionBlock:^(UMSocialPlatformType platformType, NSDictionary *userInfo) { }];
#pragma mark - UMSocialUIDelegate
/**
* 分享
*/
- (void)shareClick {
//顯示分享面板
__weak typeof(self) weakSelf = self;
[UMSocialUIManager showShareMenuViewInWindowWithPlatformSelectionBlock:^(UMSocialPlatformType platformType, NSDictionary *userInfo) {
[weakSelf shareWebPageToPlatformType:platformType];
}];
}
6.4 設(shè)置分享內(nèi)容
這里以分享網(wǎng)頁(yè)舉栗子:
//網(wǎng)頁(yè)分享
- (void)shareWebPageToPlatformType:(UMSocialPlatformType)platformType {
//創(chuàng)建分享消息對(duì)象
UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
//創(chuàng)建網(wǎng)頁(yè)內(nèi)容對(duì)象
UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:self.shopModel.name descr:@"分享內(nèi)容描述" thumImage:[NSString stringWithFormat:@"%@%@", XHH_HostUrl, self.shopModel.img]];
//設(shè)置網(wǎng)頁(yè)地址
shareObject.webpageUrl =@"http://mobile.umeng.com/social";
//分享消息對(duì)象設(shè)置分享內(nèi)容對(duì)象
messageObject.shareObject = shareObject;
//調(diào)用分享接口
[[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
if (error) {
UMSocialLogInfo(@"************Share fail with error %@*********",error);
}else{
if ([data isKindOfClass:[UMSocialShareResponse class]]) {
UMSocialShareResponse *resp = data;
//分享結(jié)果消息
UMSocialLogInfo(@"response message is %@",resp.message);
//第三方原始返回的數(shù)據(jù)
UMSocialLogInfo(@"response originalResponse data is %@",resp.originalResponse);
}else{
UMSocialLogInfo(@"response data is %@",data);
}
}
[self alertWithError:error];
}];
}
- (void)alertWithError:(NSError *)error {
NSString *result = nil;
if (!error) {
result = [NSString stringWithFormat:@"分享成功"];
}
else{
if (error) {
result = [NSString stringWithFormat:@"Share fail with error code: %d\n",(int)error.code];
}
else{
result = [NSString stringWithFormat:@"分享失敗"];
}
// result = [NSString stringWithFormat:@"分享失敗"];
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:self.shopModel.name
message:result
delegate:nil
cancelButtonTitle:@"確定"
otherButtonTitles:nil];
[alert show];
}
v6.0.1之后 分享未安裝客戶端或客戶端版本不支持時(shí)會(huì)在分享面板隱藏平臺(tái),也就是說模擬器未安裝這些平臺(tái),就會(huì)在分享面板隱藏平臺(tái)!
這個(gè)時(shí)候我們可以在點(diǎn)擊分享按鈕事件里相對(duì)應(yīng)去判斷:
手機(jī)是否安裝微信和QQ給出提示,提高用戶體驗(yàn).
if (![QQApiInterface isQQInstalled] && ![WXApi isWXAppInstalled]) { //沒有安裝微信和QQ
[MBProgressHUD showError:@"請(qǐng)安裝微信或者QQ客戶端后分享"];
} else { // 兩者皆安裝則分享
[self shareClick];
}
由于目前精簡(jiǎn)版,不能讀取這個(gè)判斷是否安裝的接口,可用
// 判斷是否安裝微信
[[UIApplicationsharedApplication]canOpenURL:[NSURLURLWithString:@"weixin://"]]
// 判斷是否安裝QQ
[[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"mqq://"]]
// 判斷是否安裝微博
[[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"weibo://"]]

之前針對(duì) iOS9寫的iOS友盟第三方登錄及分享,這里是微信 QQ 微博分享的,有需要請(qǐng)移步:iOS友盟第三方登錄及分享
分享SDK6.1之后的版本全面支持HTTPS協(xié)議,各位開發(fā)者不要使用6.1版本之前的SDK,避免后續(xù)無法通過AppStore審核!!!!!