說明:我們的分享菜單可以修改背景,裡面的圖標以及文字,顏色等,另外可以自己自定義UI,用自己的方法寫界面,寫好了之後可以調用我們以下無UI的分享方法,另外我們的UI也是開源的。
一、無 UI 分享
#import
//創建分享參數
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
#import
// 自定義分享菜單欄需要導入的頭文件
#import
#自定義分享編輯界面所需要導入的頭文件
#import
//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
#import
//先構造分享參數:
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
//添加一個自定義的平臺(非必要)
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) {}
閱讀更多 Mob開發者服務平臺 的文章