說明:我們的分享菜單可以修改背景,裡面的圖示以及文字,顔色等,另外可以自己自定義UI,用自己的方法寫界面,寫好了之後可以調用我們以下無UI的分享方法,另外我們的UI也是開源的。
一、無 UI 分享
#import <ShareSDK/ShareSDK.h>
//建立分享參數
NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
[shareParams SSDKSetupShareParamsByText:@"分享内容"
images:images //傳入要分享的圖檔
url:[NSURL URLWithString:@"http://mob.com"]
title:@"分享标題"
type:SSDKContentTypeAuto];
//進行分享
[ShareSDK share:SSDKPlatformTypeSinaWeibo //傳入分享的平台類型
parameters:shareParams
onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) { // 回調處理....}];
}
複制代碼
二、自定義分享菜單欄樣式
// 彈出分享菜單需要導入的頭檔案
#import <ShareSDK/ShareSDK.h>
#import <ShareSDKUI/ShareSDK+SSUI.h>
// 自定義分享菜單欄需要導入的頭檔案
#import <ShareSDKUI/SSUIShareActionSheetStyle.h>
#自定義分享編輯界面所需要導入的頭檔案
#import <ShareSDKUI/SSUIEditorViewStyle.h>
//1、建立分享參數(必要)
NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
NSArray* imageArray = @[[UIImage imageNamed:@"圖檔名.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 blackColor]];
// 設定分享菜單-社交平台文本顔色
[SSUIShareActionSheetStyle setItemNameColor:[UIColor whiteColor]];
// 設定分享菜單-社交平台文本字型
[SSUIShareActionSheetStyle setItemNameFont:[UIFont systemFontOfSize:10]];
//設定分享編輯界面的導航欄顔色
[SSUIEditorViewStyle setiPhoneNavigationBarBackgroundColor:[UIColor blackColor]];
//設定編輯界面标題顔色
[SSUIEditorViewStyle setTitleColor:[UIColor redColor]];
//設定取消釋出标簽文本顔色
[SSUIEditorViewStyle setCancelButtonLabelColor:[UIColor blueColor]];
[SSUIEditorViewStyle setShareButtonLabelColor:[UIColor blueColor]];
//設定分享編輯界面狀态欄風格
[SSUIEditorViewStyle setStatusBarStyle:UIStatusBarStyleLightContent];
//設定簡單分享菜單樣式
[SSUIShareActionSheetStyle setShareActionSheetStyle:ShareActionSheetStyleSimple];
//2、彈出ShareSDK分享菜單
[ShareSDK showShareActionSheet:view
items:nil
shareParams:shareParams
onShareStateChanged:^(SSDKResponseState state, SSDKPlatformType platformType, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error, BOOL end) { ...... }
複制代碼
三、跳過分享的編輯界面
設定彈出分享菜單,直接點選菜單中的平台分享(跳過分享的編輯界面)。
#import <ShareSDK/ShareSDK.h>
#import <ShareSDKUI/SSUIShareActionSheetController.h>
//先構造分享參數:
NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
[shareParams SSDKSetupShareParamsByText:@"分享内容"
images:@[[UIImage imageNamed:@"shareImg.png"]]
url:[NSURL URLWithString:@"http://mob.com"]
title:@"分享标題"
type:SSDKContentTypeAuto];
//有的平台要用戶端分享需要加此方法,例如微網誌
[shareParams SSDKEnableUseClientShare];
//調用分享的方法
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)];(加了這個方法之後可以不跳分享編輯界面,直接點選分享菜單裡的選項,直接分享)
複制代碼
四、隐藏“微信收藏”平台
[ShareSDK registerActivePlatforms:@[
// 不要使用微信總平台進行初始化
//@(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;
}
}];
複制代碼
五、(分享菜單)平台順序自定義
ShareSDK提供的UI (分享菜單) 平台順序自定義。
//分享
[ShareSDK showShareActionSheet:nil
//将要自定義順序的平台傳入items參數中
items:@[@(SSDKPlatformTypeMail),
@(SSDKPlatformTypeSMS),
@(SSDKPlatformTypeCopy),
@(SSDKPlatformTypeFacebook),
@(SSDKPlatformTypeTwitter),
@(SSDKPlatformTypeWechat),
@(SSDKPlatformTypeQQ)]
shareParams:shareParams
onShareStateChanged:^(SSDKResponseState state, SSDKPlatformType platformType, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error, BOOL end) { .......}];
複制代碼
六、自定義分享菜單項
如果想在我們的分享菜單上添加一個自己的按鈕,處理自己想要做的事件,如下所示:
#import <ShareSDKUI/SSUIShareActionSheetCustomItem.h>
//添加一個自定義的平台(非必要)
SSUIShareActionSheetCustomItem *item = [SSUIShareActionSheetCustomItem itemWithIcon:[UIImage imageNamed:@"Icon.png"]
label:@"自定義"
onClick:^{
//自定義item被點選的處理邏輯
NSLog(@"=== 自定義item被點選 ===");
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"自定義item被點選"
message:nil
delegate:nil
cancelButtonTitle:@"确定"
otherButtonTitles:nil];
[alertView show];
}];
NSArray * platforms =@[@(SSDKPlatformSubTypeQQFriend),@(SSDKPlatformSubTypeWechatSession),@(SSDKPlatformTypeTencentWeibo),item];
//再把聲明的platforms對象傳進分享方法裡的items參數裡
[ShareSDK showShareActionSheet:nil
items:platforms
shareParams:shareParams
onShareStateChanged:^(SSDKResponseState state, SSDKPlatformType platformType, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error, BOOL end) {}
複制代碼