最近在 App 中加入了分享功能,採用的是 ShareSDK v3.x。以下記錄了在專案中整合 ShareSDK 社會化分享的完整步驟。
環境:iOS 9.3、開發工具 Xcode 7.3、ShareSDK v3.x。官方提供了快速整合的主要流程(參見 mob.com 的 wiki),這裡不再贅述。由於專案只需分享到 QQ、微信與新浪微博,因此只引入對應的標頭檔與所需的庫,其餘一律移除。下面直接看初始化設定與分享動作的程式碼。
初始化時,AppID、AppKey、appSecret 要替換成自己在各平台申請到的憑證。在 activePlatforms 中,微信平台沒有使用 SSDKPlatformTypeWechat,因為它預設包含微信好友、朋友圈與微信收藏,而我們不需要收藏,所以改用子選項。
/* 初始化代碼 */
-(void) shareSDKInit{
/**
* 設置 ShareSDK 的 appKey,若尚未在官網註冊過 App,請至 mob.com/login 登入後台完成應用註冊,
* 再把產生的 AppKey 傳入此方法。第二個參數用於指定要使用哪些社交平台(以陣列傳入),
* 第三個參數在需要連接平台 SDK 時觸發,第四個參數則在配置本地平台時觸發。
*/
[ShareSDK registerApp:@"bundle id"
activePlatforms:@[ @(SSDKPlatformTypeSinaWeibo),
@(SSDKPlatformSubTypeWechatSession),
@(SSDKPlatformSubTypeWechatTimeline),
@(SSDKPlatformTypeQQ) ]
onImport:^(SSDKPlatformType platformType){
switch (platformType) {
case SSDKPlatformTypeWechat:
[ShareSDKConnector connectWeChat:[WXApi class]];
[ShareSDKConnector connectWeChat:[WXApi class] delegate:self];
break;
case SSDKPlatformTypeQQ:
[ShareSDKConnector connectQQ:[QQApiInterface class] tencentOAuthClass:[TencentOAuth class]];
break;
case SSDKPlatformTypeSinaWeibo:
[ShareSDKConnector connectWeibo:[WeiboSDK class]];
break;
default:
break;
}
}
onConfiguration:^(SSDKPlatformType platformType, NSMutableDictionary *appInfo){
switch (platformType) {
case SSDKPlatformTypeSinaWeibo:
// 設置新浪微博應用信息,authType 設為 SSO+Web 授權
[appInfo SSDKSetupSinaWeiboByAppKey:@"2122649601"
appSecret:@"4f2923d4f6657cb98a35b8e1ec8a3dr6"
redirectUri:@"https://open.weibo.com/apps/"
authType:SSDKAuthTypeBoth];
break;
case SSDKPlatformTypeTencentWeibo:
// 設置騰訊微博應用信息,只用 Web 授權
[appInfo SSDKSetupTencentWeiboByAppKey:@"801207650"
appSecret:@"ae36f4ee3946e1cbb18d6965b0b2ff5c"
redirectUri:@"https://www.sharesdk.cn"];
break;
case SSDKPlatformTypeWechat:
[appInfo SSDKSetupWeChatByAppId:@"wx8c206faf30cb4ba7"
appSecret:@"0ef244c823dc1302a91b50a99ee70e4f"];
break;
case SSDKPlatformTypeQQ:
[appInfo SSDKSetupQQByAppId:@"1105459316"
appKey:@"ACed2xqrNZimx3gF"
authType:SSDKAuthTypeBoth];
break;
default:
break;
}
}];
}
點擊分享按鈕後執行以下程式,這裡會顯示分享選單並處理分享結果。
- (void)shareShow:(NSString *)shareUrl{
/** 在簡單分享中,只要設置共用分享參數即可分享到任意平台 **/
__weak ViewController *theController = self;
// 1、創建分享參數(必要)
NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
// 打開客戶端
[shareParams SSDKEnableUseClientShare];
NSArray* imageArray = @[[UIImage imageNamed:@"shareImg.png"]];
[shareParams SSDKSetupShareParamsByText:@"分享內容"
images:imageArray
url:[NSURL URLWithString:@"https://www.star-v.com.cn"]
title:@"分享標題"
type:SSDKContentTypeAuto];
// 1.2、自定義分享平台(非必要)
NSMutableArray *activePlatforms = [NSMutableArray arrayWithArray:[ShareSDK activePlatforms]];
// 添加自定義平台(非必要)
SSUIShareActionSheetCustomItem *item = [SSUIShareActionSheetCustomItem itemWithIcon:[UIImage imageNamed:@"Icon.png"]
label:@"自定義"
onClick:^{
NSLog(@"=== 自定義 item 被點擊 ===");
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"自定義 item 被點擊" message:nil delegate:nil cancelButtonTitle:@"確定" otherButtonTitles:nil];
[alertView show];
}];
[activePlatforms addObject:item];
// 設置分享菜單欄樣式(非必要)
[SSUIShareActionSheetStyle setActionSheetBackgroundColor:[UIColor colorWithRed:100/255.0 green:100/255.0 blue:100/255.0 alpha:0.5]];
[SSUIShareActionSheetStyle setActionSheetColor:[UIColor colorWithRed:21.0/255.0 green:21.0/255.0 blue:21.0/255.0 alpha:0.8]];
[SSUIShareActionSheetStyle setCancelButtonBackgroundColor:[UIColor colorWithRed:21.0/255.0 green:21.0/255.0 blue:21.0/255.0 alpha:0.8]];
[SSUIShareActionSheetStyle setCancelButtonLabelColor:[UIColor whiteColor]];
[SSUIShareActionSheetStyle setItemNameColor:[UIColor whiteColor]];
[SSUIShareActionSheetStyle setItemNameFont:[UIFont systemFontOfSize:10]];
[SSUIShareActionSheetStyle setCurrentPageIndicatorTintColor:[UIColor colorWithRed:156/255.0 green:156/255.0 blue:156/255.0 alpha:1.0]];
[SSUIShareActionSheetStyle setPageIndicatorTintColor:[UIColor colorWithRed:62/255.0 green:62/255.0 blue:62/255.0 alpha:1.0]];
// 2、分享
SSUIShareActionSheetController *sheet = [ShareSDK showShareActionSheet:self.view
items:@[@(SSDKPlatformTypeQQ),@(SSDKPlatformSubTypeWechatSession),@(SSDKPlatformSubTypeWechatTimeline),@(SSDKPlatformTypeSinaWeibo)]
shareParams:shareParams
onShareStateChanged:^(SSDKResponseState state, SSDKPlatformType platformType, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error, BOOL end){
switch (state) {
case SSDKResponseStateBegin: {
break;
}
case SSDKResponseStateSuccess: {
// Facebook Messenger、WhatsApp 等平台擷取不到分享成功或失敗的狀態,需區別對待
if (platformType == SSDKPlatformTypeFacebookMessenger) { break; }
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享成功" message:nil delegate:nil cancelButtonTitle:@"確定" otherButtonTitles:nil];
[alertView show];
break;
}
case SSDKResponseStateFail: {
if (platformType == SSDKPlatformTypeSMS && [error code] == 201) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"分享失敗" message:@"失敗原因可能是:1、短信應用沒有設置帳號;2、設備不支持短信應用;3、短信應用在 iOS 7 以上才能發送帶附件的短信。" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
break;
} else if(platformType == SSDKPlatformTypeMail && [error code] == 201) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"分享失敗" message:@"失敗原因可能是:1、郵件應用沒有設置帳號;2、設備不支持郵件應用;" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
break;
} else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"分享失敗" message:[NSString stringWithFormat:@"%@",error] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
break;
}
break;
}
case SSDKResponseStateCancel: {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享已取消" message:nil delegate:nil cancelButtonTitle:@"確定" otherButtonTitles:nil];
[alertView show];
break;
}
default:
break;
}
}];
[sheet.directSharePlatforms addObject:@(SSDKPlatformTypeSinaWeibo)];
}
加上以上程式碼後還無法完成分享,還需要下列步驟。
加入白名單
自 iOS 9.0 起,凡涉及平台客戶端的跳轉,系統都會到 info.plist 檢查是否設定了 Scheme。若沒有對應設定,就無法跳轉到相應客戶端。因此只要整合了分享與授權,就需要配置 Scheme 白名單。
在 info.plist 新增 key:LSApplicationQueriesSchemes,型別為 Array,再把需要支援的白名單逐項加入,型別為 String。

實現 SSO 授權(無需用戶輸入密碼即可登入)
打開 Info.plist,找到 URL types(若無則新增),展開 URL types – URL Schemes,在其下分別新增各應用的 Scheme。填寫格式為:wb+appKey,例如 wb2279784657。
執行時可能出現的錯誤
App Transport Security has blocked a cleartext HTTP (https://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist file. 解法同樣是打開 Info.plist,找到 URL types 並做如下配置: