iOS ShareSDK實現分享——微信分享
- 必須新增的依賴庫如下(Xcode 7 下 *.dylib庫字尾名更改為*.tbd):
-
- SystemConfiguration.framework
- QuartzCore.framework
- CoreTelephony.framework
- libicucore.dylib
- libz.1.2.5.dylib
- Security.framework
- JavaScriptCore.framework
- libstdc++.dylib
- CoreText.framework
- 以下依賴庫根據社交平臺新增:
- 微信SDK依賴庫 libsqkute3.dylib
設定appKey
開啟AppDelegate.m檔案。匯入標頭檔案實現代理#import <ShareSDK/ShareSDK.h> #import "WXApi.h"
@interface GAShareUtil ()<WXApiDelegate>
@end
在- (BOOL)application: didFinishLaunchingWithOptions:方法中呼叫registerApp方法來初始化SDK並且初始化第三方平臺/** * 設定ShareSDK的appKey,如果尚未在ShareSDK官網註冊過App,請移步到http://mob.com/login 登入後臺進行應用註冊 * 在將生成的AppKey傳入到此方法中。 * 方法中的第二個第三個引數為需要連線社交平臺SDK時觸發, * 在此事件中寫入連線程式碼。第四個引數則為配置本地社交平臺時觸發,根據返回的平臺型別來配置平臺資訊。 * 如果您使用的時服務端託管平臺資訊時,第二、四項引數可以傳入nil,第三項引數則根據服務端託管平臺來決定要連線的社交SDK。 */ [ShareSDK registerApp:@"iosv1101" activePlatforms:@[ @(SSDKPlatformTypeWechat)] onImport:^(SSDKPlatformType platformType){ switch (platformType){ case SSDKPlatformTypeWechat: [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:@"" // appSecret:@"" // redirectUri:@"" // authType:SSDKAuthTypeBoth]; // break; case SSDKPlatformTypeWechat: [appInfo SSDKSetupWeChatByAppId:@"" appSecret:@""]; break; default: break; } }];
然後開啟下圖位置,在URL Types中新增微信AppID
分享程式碼
1 簡單分享 --無UI分享
/** * 簡單分享 */ - (void)simplyShare{ /** * 在簡單分享中,只要設定共有分享引數即可分享到任意的社交平臺(前提是在AppDelegate中成功註冊) **/ __weak SHWeiXinViewController *theController = self; [self showLoadingView:YES]; //建立分享引數 NSMutableDictionary *shareParams = [NSMutableDictionary dictionary]; //可以是單張圖片也可以是多張。 NSArray* imageArray = @[[UIImage imageNamed:@"shareImg.png"]]; if (imageArray) { [shareParams SSDKSetupShareParamsByText:@"分享內容" images:imageArray url:[NSURL URLWithString:@"http://www.mob.com"] title:@"分享標題" type:SSDKContentTypeAuto]; //進行分享 [ShareSDK share:SSDKPlatformSubTypeWechatSession parameters:shareParams // [ShareSDK share:SSDKPlatformTypeSinaWeibo // parameters:shareParams onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) { [theController showLoadingView:NO]; [theController.wxtableView reloadData]; switch (state) {//判斷分享是否成功 case SSDKResponseStateSuccess:{ UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享成功" message:nil delegate:nil cancelButtonTitle:@"確定" otherButtonTitles:nil]; [alertView show]; break; } case SSDKResponseStateFail:{ UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享失敗" message:[NSString stringWithFormat:@"%@", error] delegate:nil cancelButtonTitle:@"確定" otherButtonTitles:nil]; [alertView show]; break; } case SSDKResponseStateCancel:{ UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享已取消" message:nil delegate:nil cancelButtonTitle:@"確定" otherButtonTitles:nil]; [alertView show]; break; } default: break; } }]; } }
/**
* 設定分享引數
* @param text 文字
* @param url 網頁路徑/應用路徑
* @param title 標題
* @param type 分享型別
*/
- (void)SSDKSetupShareParamsByText:(NSString *)text
images:(id)images
url:(NSURL *)url
title:(NSString *)title
type:(SSDKContentType)type;
SSDKContentType 用來設定分享型別。如下圖所示 相同程式碼,不同的型別對應不同效果
當SSDKContentType設定為SSDKContentTypeAuto會自動判斷分享的型別,類顯示不同的介面。
2 分享選單
顯示分享選單- (void)showShareActionSheet:(UIView *)view{
/**
* 在簡單分享中,只要設定共有分享引數即可分享到任意的社交平臺
**/
__weak SHWeiXinViewController *theController = self;
//1、建立分享引數(必要)
NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
NSArray* imageArray = @[[UIImage imageNamed:@"shareImg.png"]];
[shareParams SSDKSetupShareParamsByText:@"分享內容"
images:imageArray
url:[NSURL URLWithString:@"http://www.mob.com"]
title:@"分享標題"
type:SSDKContentTypeAuto];
//2、分享
[ShareSDK showShareActionSheet:view
items:nil
shareParams:shareParams
onShareStateChanged:^(SSDKResponseState state, SSDKPlatformType platformType, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error, BOOL end) {
switch (state) {
case SSDKResponseStateBegin:{
[theController showLoadingView:YES];
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;
}
if (state != SSDKResponseStateBegin){
[theController showLoadingView:NO];
[theController.wxtableView reloadData];
}
}];
}
顯示分享選單的方法
+ (SSUIShareActionSheetController *)showShareActionSheet:(UIView *)view
items:(NSArray *)items
shareParams:(NSMutableDictionary *)shareParams
onShareStateChanged:(SSUIShareStateChangedHandler)shareStateChangedHandler;
當items引數為nil時,則會顯示已整合的平臺列表,可以通過設定items只顯示自己需要的平臺。其中SSDKPlatformTypeWechat預設包括微信好友,朋友圈和微信收藏。可以通過設定這三個引數可以讓各別顯示和隱藏。
SSDKPlatformSubTypeWechatSession,SSDKPlatformSubTypeWechatTimeline,SSDKPlatformSubTypeWechatFav
[ShareSDK showShareActionSheet:nil
//將要自定義順序的平臺傳入items引數中
items:@[@(SSDKPlatformTypeWechatTimeLine)
@(SSDKPlatformTypeWechatSession)]
shareParams:shareParams
onShareStateChanged:^(SSDKResponseState state, SSDKPlatformType platformType, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error, BOOL end) { .......}];
效果圖所示:
3 自定義分享選單欄樣式
// 彈出分享選單需要匯入的標頭檔案
#import <ShareSDKUI/ShareSDK+SSUI.h>
// 自定義分享選單欄需要匯入的標頭檔案
#import <ShareSDKUI/SSUIShareActionSheetStyle.h>
//1、建立分享引數(必要)
NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
NSArray* imageArray = @[[UIImage imageNamed:@"shareImg.png"]];
(注意:圖片要在Xcode左邊目錄裡面,名稱必須要傳正確,或者是本地沙盒中的檔案,如果要分享網路圖片,可以這樣傳iamge引數 images:@[@"http://mob.com/Assets/images/logo.png?v=20150320"])
[shareParams SSDKSetupShareParamsByText:@"分享內容"
images:imageArray
url:[NSURL URLWithString:@"http://mob.com"]
title:@"分享標題"
type:SSDKContentTypeAuto];
// 設定分享選單欄樣式(非必要)
// 設定分享選單的背景顏色
[SSUIShareActionSheetStyle setActionSheetBackgroundColor:[UIColor colorWithRed:249/255.0 green:0/255.0 blue:12/255.0 alpha:0.5]];
// 設定分享選單顏色
[SSUIShareActionSheetStyle setActionSheetColor:[UIColor colorWithRed:21.0/255.0 green:21.0/255.0 blue:21.0/255.0 alpha:1.0]];
// 設定分享選單-取消按鈕背景顏色
[SSUIShareActionSheetStyle setCancelButtonBackgroundColor:[UIColor colorWithRed:21.0/255.0 green:21.0/255.0 blue:21.0/255.0 alpha:1.0]];
// 設定分享選單-取消按鈕的文字顏色
[SSUIShareActionSheetStyle setCancelButtonLabelColor:[UIColor yellowColor]];
// 設定分享選單-社交平臺文字顏色
[SSUIShareActionSheetStyle setItemNameColor:[UIColor redColor]];
// 設定分享選單-社交平臺文字字型
[SSUIShareActionSheetStyle setItemNameFont:[UIFont systemFontOfSize:11]];
//2、彈出ShareSDK分享選單
[ShareSDK showShareActionSheet:view
items:@[@(SSDKPlatformTypeWechatTimeLine)
@(SSDKPlatformTypeWechatSession)]
shareParams:shareParams
onShareStateChanged:^(SSDKResponseState state, SSDKPlatformType platformType, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error, BOOL end) { ...... }
效果圖:4 跳過分享的編輯介面
彈出分享選單,直接點選選單中的平臺分享(跳過分享的編輯介面)。
//先構造分享引數:
NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
[shareParams SSDKSetupShareParamsByText:@"分享內容"
images:@[[UIImage imageNamed:@"shareImg.png"]]
url:[NSURL URLWithString:@"http://mob.com"]
title:@"分享標題"
type:SSDKContentTypeAuto];
//呼叫分享的方法
SSUIShareActionSheetController *sheet = [ShareSDK showShareActionSheet:view
items:nil
shareParams:shareParams
onShareStateChanged:^(SSDKResponseState state, SSDKPlatformType platformType, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error, BOOL end) {
switch (state) {
case SSDKResponseStateSuccess:
NSLog(@"分享成功!");
break;
case SSDKResponseStateFail:
NSLog(@"分享失敗%@",error);
break;
case SSDKResponseStateCancel:
NSLog(@"分享已取消");
break;
default:
break;
}
}];
//刪除和新增平臺示例
[sheet.directSharePlatforms removeObject:@(SSDKPlatformTypeWechat)];
//(預設微信,QQ,QQ空間都是直接跳客戶端分享,加了這個方法之後,可以跳分享編輯介面分享)
[sheet.directSharePlatforms addObject:@(SSDKPlatformTypeSinaWeibo)];
//(其它平臺預設會跳分享編輯介面,加了這個方法之後可以不跳分享編輯介面,直接點選分享選單裡的選項,直接分享)
5 不顯示分享平臺顯示分享編輯選單
[ShareSDK showShareEditor:SSDKPlatformTypeWechat
otherPlatformTypes:@[@(SSDKPlatformTypeSinaWeibo),@(SSDKPlatformTypeTencentWeibo)]
shareParams:shareParams
onShareStateChanged:^(SSDKResponseState state, SSDKPlatformType platformType, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error, BOOL end)
{}];
顯示分享編輯框,只有微信,不顯示otherPlatformTypes中的其它選項。
6 自定義編輯框
匯入
#import <ShareSDK/ShareSDK.h>
#import <ShareSDKUI/SSUIEditorViewStyle.h>
NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
[shareParams SSDKSetupShareParamsByText:@"分享內容..."
images:imageArray
url:[NSURL URLWithString:@"http://mob.com"]
title:@"分享標題"
type:SSDKContentTypeImage];
//自定義編輯區樣式(非必要)
//設定iPhone導航欄顏色
[SSUIEditorViewStyle setiPhoneNavigationBarBackgroundColor:[UIColor redColor]];
//設定編輯介面背景顏色
[SSUIEditorViewStyle setContentViewBackgroundColor:[UIColor cyanColor]];
//設定標題
[SSUIEditorViewStyle setTitle:@"微信分享"];
//設定取消按鈕標籤
[SSUIEditorViewStyle setCancelButtonLabel:@"算了吧"];
//設定取消按鈕標籤文字顏色
[SSUIEditorViewStyle setCancelButtonLabelColor:[UIColor blackColor]];
[ShareSDK showShareEditor:SSDKPlatformTypeWechat
otherPlatformTypes:@[@(SSDKPlatformTypeSinaWeibo),@(SSDKPlatformTypeTencentWeibo)]
shareParams:shareParams
onShareStateChanged:^(SSDKResponseState state, SSDKPlatformType platformType, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error, BOOL end)
{
}];
7 隱藏“微信收藏”平臺
方法有兩種
1 在appDeleagete中修改,這樣比較方便。
[ShareSDK registerApp:@"XXXXXXX"
activePlatforms:@[
// 不要使用微信總平臺進行初始化
//@(SSDKPlatformTypeWechat),
// 使用微信子平臺進行初始化,即可
@(SSDKPlatformSubTypeWechatSession),
@(SSDKPlatformSubTypeWechatTimeline),
]
onImport:^(SSDKPlatformType platformType) {
switch (platformType)
{
case SSDKPlatformTypeWechat:
[ShareSDKConnector connectWeChat:[WXApi class]];
break;
default:
break;
}
}
onConfiguration:^(SSDKPlatformType platformType, NSMutableDictionary *appInfo) {
switch (platformType)
{
case SSDKPlatformTypeWechat:
[appInfo SSDKSetupWeChatByAppId:@"wx4868b35061f87885"
appSecret:@"64020361b8ec4c99936c0e3999a9f249"];
break;
default:
break;
}
}];
2 在某些使用的地方,將SSDKPlatformTypeWechat修改成微信子平臺,去掉微信收藏。這個是針對個別地方隱藏。
8 平臺內容定製
/**
* 在定製平臺內容分享中,除了設定共有的分享引數外,還可以為特定的社交平臺進行內容定製,
* 如:其他平臺分享的內容為“分享內容”,但微信需要在原有的“分享內容”文字後面加入一條連結,則可以如下做法:
**/
__weak SHWeiXinViewController *theController = self;
[theController showLoadingView:YES];
//建立分享引數
NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
NSArray* imageArray = @[[UIImage imageNamed:@"shareImg.png"]];
if (imageArray) {
[shareParams SSDKSetupShareParamsByText:@"分享內容"
images:imageArray
url:[NSURL URLWithString:@"http://mob.com"]
title:@"分享標題"
type:SSDKContentTypeImage];
[shareParams SSDKSetupWeChatParamsByText:@"分享內容 http://mob.com"
title:@"分享標題"
url:[NSURL URLWithString:@"http://mob.com"]
thumbImage:[UIImage imageNamed:@"shareImg.png"]
image:[UIImage imageNamed:@"shareImg.png"]
musicFileURL:nil
extInfo:nil
fileData:nil
emoticonData:nil
sourceFileExtension:nil
sourceFileData:nil
type:SSDKContentTypeAuto
forPlatformSubType:SSDKPlatformSubTypeWechatSession];
//進行分享
[ShareSDK share:SSDKPlatformTypeWechat
parameters:shareParams
onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) {
}];
9 分享本地檔案(mp3、mp4、docx、pdf等)
NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
UIImage *imageThumb = [UIImage imageNamed:@"Icon.png"];
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"cattest" ofType:@"mp4"];
[shareParams SSDKSetupWeChatParamsByText:@"視訊分享。。。"
title:@"視訊分享"
url:nil
thumbImage:imageThumb
image:imageThumb
musicFileURL:nil
extInfo:nil
fileData:nil
emoticonData:nil
sourceFileExtension:@"mp4"
sourceFileData:filePath
type:SSDKContentTypeFile
forPlatformSubType:SSDKPlatformSubTypeWechatSession];
//進行分享
[ShareSDK share:SSDKPlatformSubTypeWechatSession
parameters:shareParams
onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) {
}];
分享檔案時:
* 設定type為SSDKContentTypeFile(例如.mp3、.mp4、.pdf、.docx的分享),設定title、sourceFileExtension、sourceFileData,以及thumbImage引數,如果尚未設定thumbImage則會從image引數中讀取圖片並對圖片進行縮放操作引數
*/
注意(分享失敗的兩個坑):檔案大小不能超過10M,thumbImage圖片大小不能超過32k,最好是寬高相等。
10 搖一搖分享
導包
#import <ShareSDK/ShareSDK.h>
#import <ShareSDKExtension/SSEShareHelper.h>
/**
* 使用ShareSDKExtension外掛可以實現搖一搖分享
* 通過使用SSEShareHelper可以呼叫開啟/結束搖一搖分享方法,在方法的onWillShareHandler引數中可以處理搖一搖後的分享處理操作。
*
* 小技巧:
* 當取得搖一搖事件通知後,如果shareHandler入口不滿足分享需求(如需要彈出分享選單而不是直接分享),可以不呼叫shareHandler進行分享,而是在block中寫入自定義的分享操作。
* 這樣的話搖一搖分享介面實質只充當捕獲搖一搖通知的功能。
**/
__weak SHWeiXinViewController *theController = self;
//開啟搖一搖分享功能
[SSEShareHelper beginShakeShare:nil
onEndSake:nil
onWillShareHandler:^(SSEShareHandler shareHandler){
NSLog(@"======開始分享");
//構造分享引數
NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
NSArray* imageArray = @[[UIImage imageNamed:@"shareImg.png"]];
if (imageArray){
[shareParams SSDKSetupShareParamsByText:@"搖著搖著就可以分享出去了,使用ShareSDK分享就是便捷方便。"
images:imageArray
url:nil
title:nil
type:SSDKContentTypeImage];
//顯示等待介面
[theController showLoadingView:YES];
//回撥分享
if (shareHandler){
shareHandler (SSDKPlatformTypeWechat, shareParams);
}
}
}onStateChanged:^(SSDKResponseState state,NSDictionary *userData,
SSDKContentEntity *contentEntity, NSError *error) {
}];
iOS 9 異常
App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist file.
問題描述:在iOS9下,系統預設會攔截對http協議介面的訪問,因此無法獲取http協議介面的資料。對ShareSDK來說,具體表現可能是,無法授權、分享、獲取使用者資訊等。
還可能造成我們的編輯介面裡傳http之類的網路圖片的時候,我們的編輯介面不會顯示圖片截圖,解決的辦法或者全面關閉https,允許使用http請求;或者把圖片的域新增;又或者選擇使用https的圖片
解決方案(以下方法2選1):
(1)、暫時退回到http協議。
具體方法:
在專案的info.plist中新增一個Key:NSAppTransportSecurity,型別為字典型別。
然後給它新增一個Key:NSAllowsArbitraryLoads,型別為Boolean型別,值為YES;
(2)http://wiki.mob.com/ios9-對sharesdk的影響(適配ios-9必讀)/