本文使用第三方mob進行分享,mob開放平臺http://www.mob.com 下載所需的SDK,然后這里分享了微信,qq,新浪微博,豆瓣
1.1 將sdk導入項目中,具體細節參見mob官網,如圖1.1
圖1.1
1.2 添加白名單如圖1.2
圖1.2
1.2 添加各種依賴庫如圖1.3 (為避免日后更新升級,添加依賴庫最好到mob官網查看)
圖1.3
1.4 添加 url type 如圖1.4(這里微信 qq 新浪需要)
圖1.4
2 在appdelegte里面配置shareSDK的參數
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[[[JFShareFunction alloc] init] setShareSDK]; // 設置mob分享 此方法為新建類 在下面
return YES;
}
#import <Foundation/Foundation.h>
// 分享
#import <ShareSDK/ShareSDK.h>
#import <ShareSDKUI/ShareSDK+SSUI.h>
@interface JFShareFunction : NSObject
/**
設置shareSDK
*/
- (void)setShareSDK;
/**
share默認分享菜單
@param imageArray 分享的圖片
@param urlString 分享的鏈接
@param text 分享的內容
@param title 分享的標題
*/
- (void)shareInfomationWithImageArray:(NSArray *)imageArray url:(NSString *)urlString text:(NSString *)text title:(NSString *)title;
/**
自定義分享菜單
@param imageArray 分享的圖片
@param urlString 分享的鏈接
@param text 分享的內容
@param title 分享的標題
*/
- (void)shareCustomInfomationWithImageArra:(NSArray *)imageArray url:(NSString *)urlString text:(NSString *)text title:(NSString *)title;
@end
2.1 先看一下mob默認的分享樣式
#import "JFShareFunction.h"
#import <ShareSDKUI/SSUIEditorViewStyle.h>
#import <ShareSDKUI/ShareSDK+SSUI.h>
// 自定義分享菜單欄需要導入的頭文件
#import <ShareSDKUI/SSUIShareActionSheetStyle.h>
#import <ShareSDK/ShareSDK.h>
#import "JFShareCustom.h"
#import "WeiboSDK.h"
@class SSUIShareActionSheetItem;
@interface JFShareFunction ()
@property (strong , nonatomic) NSArray *PlatformTypeArray;
@end
@implementation JFShareFunction
- (NSArray *)PlatformTypeArray{
_PlatformTypeArray = @[
@(SSDKPlatformTypeSinaWeibo),
@(SSDKPlatformTypeDouBan),
@(SSDKPlatformTypeMail),
@(SSDKPlatformTypeSMS),
@(SSDKPlatformSubTypeWechatSession),
@(SSDKPlatformSubTypeWechatTimeline),
@(SSDKPlatformTypeQQ),
];
return _PlatformTypeArray;
}
- (void)setShareSDK{
[ShareSDK registerActivePlatforms:self.PlatformTypeArray
onImport:^(SSDKPlatformType platformType)
{
switch (platformType)
{
case SSDKPlatformTypeWechat:
[ShareSDKConnector connectWeChat:[WXApi class]];
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:KVendor_Weibo_AppKey // 微博
appSecret:KVendor_Weibo_App_Secret
redirectUri:KVendor_Weibo_redirectUri
authType:SSDKAuthTypeBoth];
break;
case SSDKPlatformTypeWechat: // 微信
[appInfo SSDKSetupWeChatByAppId:KVendor_WeChat_AppID //wx272b73bc8474d024
appSecret:KVendor_WeChat_App_Secret];
break;
case SSDKPlatformTypeQQ: // qq
[appInfo SSDKSetupQQByAppId:KVendor_QQ_AppID
appKey:KVendor_QQ_App_key
authType:SSDKAuthTypeBoth];
break;
case SSDKPlatformTypeDouBan: // 豆瓣
[appInfo SSDKSetupDouBanByApiKey:KVendor_Douban_AppID
secret:KVendor_Douban_App_key
redirectUri:KVendor_Douban_redirectUri];
break;
default:
break;
}
}];
}
// 分享
- (void)shareInfomationWithImageArray:(NSArray *)imageArray url:(NSString *)urlString text:(NSString *)text title:(NSString *)title {
//poster
NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
[shareParams SSDKSetupShareParamsByText:text
images:imageArray
url:[NSURL URLWithString:urlString]
title:title
type:SSDKContentTypeAuto];
//有的平臺要客戶端分享需要加此方法,例如微博
[shareParams SSDKEnableUseClientShare];
SSUIShareActionSheetController *sheet = [ShareSDK showShareActionSheet:nil
items:self.PlatformTypeArray
shareParams:shareParams
onShareStateChanged:^(SSDKResponseState state, SSDKPlatformType platformType, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error, BOOL end) {
switch (state) {
case SSDKResponseStateSuccess:
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享成功"
message:nil
delegate:nil
cancelButtonTitle:@"確定"
otherButtonTitles:nil];
[alertView show];
break;
}
case SSDKResponseStateFail:
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"分享失敗"
message:[NSString stringWithFormat:@"%@",error.userInfo[@"error_message"]] delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil, nil];
NSLog(@"錯誤信息-----------%@",error.userInfo[@"error_message"]);
[alert show];
break;
}
default:
break;
}
}];
// 跳過編輯頁面直接分享 編輯頁面如圖2.4
// [sheet.directSharePlatforms addObject:@(SSDKPlatformTypeSinaWeibo)];
// [sheet.directSharePlatforms addObject:@(SSDKPlatformTypeDouBan)];
// [sheet.directSharePlatforms addObject:@(SSDKPlatformTypeMail)];
// [sheet.directSharePlatforms addObject:@(SSDKPlatformTypeSMS)];
}
@end
2.2 點擊分享按鈕分享標題、圖片、鏈接等
NSArray *imageArra = [NSArray arrayWithObjects:[UIImage imageNamed:@"poster"],nil];
[[[JFShareFunction alloc] init] shareInfomationWithImageArray:imageArra url:@"http://test-m-stg.ppppoints.com/jfmall/index.htm" text:@"這是分享內容" title:@"這是分享title"];
2.3 點擊分享按鈕的試圖如圖2.3 ,這里mob的sdk判斷了手機里面有沒有安裝需要分享的平臺的客服端,如果沒有,在分享菜單中就不顯示,有就一排最多四個顯示
圖2.3
2.4 點擊微博分享,會默認跳出編輯頁面 ,如圖2.4
圖2.4
點擊右上角share,跳轉對應的web端或者客戶端如圖 2.4.1
圖2.4.1
3 以上就集成好了share,如果想自定義分享菜單,如圖3.1
圖3.1
3.1 自建自定義分享頁面類
#import <Foundation/Foundation.h>
@interface JFShareCustom : NSObject
+(void)shareWithContent:(id)publishContent;//自定義分享界面
@end
#import "JFShareCustom.h"
#import <QuartzCore/QuartzCore.h>
#import <ShareSDK/ShareSDK.h>
#import <ShareSDK/ShareSDK.h>
#import <ShareSDKUI/ShareSDK+SSUI.h>
#import <ShareSDKConnector/ShareSDKConnector.h>
//騰訊開放平臺(對應QQ和QQ空間)SDK頭文件
#import <TencentOpenAPI/TencentOAuth.h>
#import <TencentOpenAPI/QQApiInterface.h>
//微信SDK頭文件
#import "WXApi.h"
#define SYSTEM_VERSION [[UIDevice currentDevice].systemVersion floatValue]
#define KWidth_Scale [UIScreen mainScreen].bounds.size.width/375.0f
// 顏色
#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
// 適配
#define DevicesScale ([UIScreen mainScreen].bounds.size.height==480?1.00:[UIScreen mainScreen].bounds.size.height==568?1.00:[UIScreen mainScreen].bounds.size.height==667?1.17:1.29)
@implementation JFShareCustom
static id _publishContent;//類方法中的全局變量這樣用(類型前面加static)
static UIVisualEffectView *_effectView;
+(void)shareWithContent:(id)publishContent/*只需要在分享按鈕事件中 構建好分享內容publishContent傳過來就好了*/
{
_publishContent = publishContent;
UIWindow *window = [UIApplication sharedApplication].keyWindow;
UIBlurEffect * blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
_effectView = [[UIVisualEffectView alloc]initWithEffect:blur];
_effectView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
[window addSubview:_effectView];
_effectView.contentView.alpha = 0;
_effectView.alpha = 0.3;
/**
點擊退出手勢
*/
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismiss)];
[_effectView addGestureRecognizer:tap];
/**
Share Content
*/
UIView *shareView = [[UIView alloc] initWithFrame:CGRectMake(0, (SCREEN_HEIGHT-249 * DevicesScale), SCREEN_WIDTH, 249*DevicesScale)];
shareView.tag = 441;
shareView.backgroundColor = [UIColor colorWithRed:10 green:10 blue:10 alpha:1];
[window addSubview:shareView];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, shareView.frame.size.width, 45*KWidth_Scale)];
titleLabel.text = @"分享到";
titleLabel.textAlignment = NSTextAlignmentCenter;
titleLabel.font = [UIFont systemFontOfSize:18*KWidth_Scale];
titleLabel.textColor = UIColorFromRGB(0x374552);
titleLabel.backgroundColor = [UIColor clearColor];
[shareView addSubview:titleLabel];
NSArray *btnImages = @[@"sns_icon_24",@"sns_icon_22",@"sns_icon_6",@"sns_icon_23",@"sns_icon_1",@"sns_icon_18",@"sns_icon_19",@"sns_icon_5"];
NSArray *btnTitles = @[ @"QQ", @"微信好友", @"QQ空間", @"朋友圈",@"微博",@"郵件",@"短信",@"豆瓣"];
for (NSInteger i=0; i<btnImages.count; i++) {
CGFloat bt_width = 85 * DevicesScale;
if(SCREEN_HEIGHT == 568) {
bt_width = 95 *DevicesScale;
}
CGFloat gap = (shareView.frame.size.width - 4*bt_width) / 5.0;
CGFloat top = 0.0f;
if (i<4) {
top = 0*KWidth_Scale;
}
else{
top = 0*KWidth_Scale + bt_width;
}
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(gap * (i % 4 + 1) + i % 4 * bt_width, titleLabel.frame.origin.y + titleLabel.frame.size.height+top+i/4*(titleLabel.frame.origin.y+10), bt_width, bt_width)];
[button setImage:[UIImage imageNamed:btnImages[i]] forState:UIControlStateNormal]; // @"home13"
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, bt_width - 10, bt_width, 20)];
lbl.font = [UIFont systemFontOfSize:12*DevicesScale];
lbl.textAlignment = NSTextAlignmentCenter;
lbl.textColor = UIColorFromRGB(0x374552);
lbl.text = btnTitles[i];
[button addSubview:lbl];
button.tag = 331+i;
[button addTarget:self action:@selector(shareBtnClick:) forControlEvents:UIControlEventTouchUpInside];
[shareView addSubview:button];
}
//為了彈窗不那么生硬,這里加了個簡單的動畫
shareView.transform = CGAffineTransformMakeScale(1/300.0f, 1/270.0f);
[UIView animateWithDuration:0.35f animations:^{
shareView.transform = CGAffineTransformMakeScale(1, 1);
_effectView.backgroundColor = [UIColor grayColor];
} completion:^(BOOL finished) {
}];
}
+(void)shareBtnClick:(UIButton *)btn
{
int shareType = 0;
id publishContent = _publishContent;
// [MBProgressHUD showHUD];
if (btn.tag == 339) {
return;
}
switch (btn.tag) {
case 331: {
shareType = SSDKPlatformSubTypeQQFriend;
}
break;
case 332: {
shareType = SSDKPlatformSubTypeWechatSession;
}
break;
case 333: {
shareType = SSDKPlatformSubTypeQZone;
}
break;
case 334: {
shareType = SSDKPlatformSubTypeWechatTimeline;
}
break;
case 335: {
shareType = SSDKPlatformTypeSinaWeibo;
}
break;
case 336: {
shareType = SSDKPlatformTypeMail;
}
break;
case 337: {
shareType = SSDKPlatformTypeSMS;
}
break;
case 338: {
shareType = SSDKPlatformTypeDouBan;
}
break;
default:
break;
}
[self dismiss];
// 默認有編輯頁面
[ShareSDK showShareEditor:shareType
otherPlatformTypes:nil
shareParams:publishContent
onShareStateChanged:^(SSDKResponseState state, SSDKPlatformType platformType, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error, BOOL end){
// [MBProgressHUD dismiss];
switch (state) {
case SSDKResponseStateSuccess:
{
[UIAlertView showWithTitle:@"分享成功" message:@"" cancelButtonTitle:@"確定"];
break;
}
case SSDKResponseStateFail:
{
NSString *failMessage = [error.userInfo[@"error_message"] formatString];
if (failMessage.length == 0) {
failMessage = [error.userInfo[@"user_data"][@"msg"] formatString];
}
[UIAlertView showWithTitle:@"分享失敗" message:failMessage cancelButtonTitle:@"OK"];
break;
}
default:
break;
}
}];
//
// 跳過編輯頁面
// [ShareSDK share:shareType parameters:publishContent onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) {
// [MBProgressHUD dismiss];
// switch (state) {
// case SSDKResponseStateSuccess:
// {
// [UIAlertView showWithTitle:@"分享成功" message:@"" cancelButtonTitle:@"確定"];
// break;
// }
// case SSDKResponseStateFail:
// {
// NSString *failMessage = [error.userInfo[@"error_message"] formatString];
// if (failMessage.length == 0) {
// failMessage = [error.userInfo[@"user_data"][@"msg"] formatString];
// }
// [UIAlertView showWithTitle:@"分享失敗" message:failMessage cancelButtonTitle:@"OK"];
// break;
// }
// default:
// break;
// }
// }];
}
+ (void)dismiss {
UIWindow *window = [UIApplication sharedApplication].keyWindow;
UIView *shareView = [window viewWithTag:441];
//為了彈窗不那么生硬,這里加了個簡單的動畫
shareView.transform = CGAffineTransformMakeScale(1, 1);
[UIView animateWithDuration:0.35f animations:^{
shareView.transform = CGAffineTransformMakeScale(1/300.0f, 1/270.0f);
_effectView.contentView.alpha = 0;
} completion:^(BOOL finished) {
[shareView removeFromSuperview];
[_effectView removeFromSuperview];
}];
}
@end
以下有幾個需要注意的地方,使用默認的actionSheet彈出分享試圖的時候,可以設置跳過編輯頁面
SSUIShareActionSheetController *sheet = [ShareSDK showShareActionSheet:nil
items:self.PlatformTypeArray
shareParams:shareParams
onShareStateChanged:^(SSDKResponseState state, SSDKPlatformType platformType, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error, BOOL end) {
// 這里可以判斷點擊的是哪個平臺
if (platformType == SSDKPlatformTypeDouBan) {
// 做一些事情
}
// 跳過編輯頁面直接分享 (打開注釋即可)
// [sheet.directSharePlatforms addObject:@(SSDKPlatformTypeSinaWeibo)];
// [sheet.directSharePlatforms addObject:@(SSDKPlatformTypeDouBan)];
// [sheet.directSharePlatforms addObject:@(SSDKPlatformTypeMail)];
// [sheet.directSharePlatforms addObject:@(SSDKPlatformTypeSMS)];
}
有幾個需要注意的地方:
1 郵件分享 :如果沒有設置好郵件的賬號密碼,默認是會分享并且回調回來顯示分享成功,但是實際上可能郵件并沒有發送成功,mob客服說現在沒法監聽郵件是否發送成功
2 豆瓣分享 :不管手機有沒有下載豆瓣app,都默認網頁版分享豆瓣,沒法掉起豆瓣的app
3 新的自定義頁面如圖 3.1
圖3.1
#import <Foundation/Foundation.h>
#import <ShareSDK/ShareSDK.h>
#import <ShareSDKUI/ShareSDK+SSUI.h>
#import "JFShareInformation.h"
@interface JFShareFunction : NSObject
/**
設置shareSDK
*/
- (void)setShareSDK;
/**
自定義分享菜單
@param information 分享的信息
*/
- (void)shareCustomInfomationWithmodel:(JFShareInformation *)information;
@end
#import "JFShareFunction.h"
#import <ShareSDK/ShareSDK.h>
#import "JFShareCustom.h"
@class SSUIShareActionSheetItem;
@interface JFShareFunction ()
@property (strong , nonatomic) NSArray *PlatformTypeArray;
@end
@implementation JFShareFunction
- (NSArray *)PlatformTypeArray{
_PlatformTypeArray = @[
@(SSDKPlatformTypeSinaWeibo),
@(SSDKPlatformTypeDouBan),
@(SSDKPlatformTypeMail),
@(SSDKPlatformTypeSMS),
@(SSDKPlatformSubTypeWechatSession),
@(SSDKPlatformSubTypeWechatTimeline),
@(SSDKPlatformTypeQQ),
];
return _PlatformTypeArray;
}
- (void)setShareSDK{
[ShareSDK registerActivePlatforms:self.PlatformTypeArray
onImport:^(SSDKPlatformType platformType)
{
switch (platformType)
{
case SSDKPlatformTypeWechat:
[ShareSDKConnector connectWeChat:[WXApi class]];
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:KVendor_Weibo_AppKey // 微博
appSecret:KVendor_Weibo_App_Secret
redirectUri:KVendor_Weibo_redirectUri
authType:SSDKAuthTypeBoth];
break;
case SSDKPlatformTypeWechat: // 微信
[appInfo SSDKSetupWeChatByAppId:KVendor_WeChat_AppID //wx272b73bc8474d024
appSecret:KVendor_WeChat_App_Secret];
break;
case SSDKPlatformTypeQQ: // qq
[appInfo SSDKSetupQQByAppId:KVendor_QQ_AppID
appKey:KVendor_QQ_App_key
authType:SSDKAuthTypeBoth];
break;
case SSDKPlatformTypeDouBan: // 豆瓣
[appInfo SSDKSetupDouBanByApiKey:KVendor_Douban_AppID
secret:KVendor_Douban_App_key
redirectUri:KVendor_Douban_redirectUri];
break;
default:
break;
}
}];
}
- (void)shareCustomInfomationWithmodel:(JFShareInformation *)information {
NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
[shareParams SSDKSetupShareParamsByText:information.content
images:information.imageurl
url:[NSURL URLWithString:information.shareurl]
title:information.title
type:SSDKContentTypeAuto];
NSString *content = [NSString stringWithFormat:@"%@%@",information.content,information.shareurl];
// 定制新浪微博的分享內容 新浪微博的url要放在content后面,不能放在url里面
[shareParams SSDKSetupSinaWeiboShareParamsByText:content
title:information.title
image:information.imageurl
url:nil
latitude:0
longitude:0
objectID:nil
type:SSDKContentTypeAuto];
//調用自定義分享
[JFShareCustom shareWithContent:shareParams];
}
@end
#import <UIKit/UIKit.h>
/**
分享菜單自定義button
*/
@interface JFShareButton : UIButton
@end
#import "JFShareButton.h"
@implementation JFShareButton
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
self.titleLabel.textColor = [UIColor darkGrayColor];
self.titleLabel.textAlignment = NSTextAlignmentCenter;
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
CGFloat w = self.frame.size.width;
CGFloat h = self.frame.size.height;
self.titleLabel.frame = CGRectMake(0, h - 33/2*kDeviceWidthScaleToiPhone6, w, 33/2*kDeviceWidthScaleToiPhone6);
if (self.tag == 332) {
self.titleLabel.frame = CGRectMake(-10*kDeviceWidthScaleToiPhone6, h - 33/2*kDeviceWidthScaleToiPhone6, w+20*kDeviceWidthScaleToiPhone6, 33/2*kDeviceWidthScaleToiPhone6);
}
self.imageView.frame = CGRectMake((w - 50*kDeviceWidthScaleToiPhone6)/2, 0, 50*kDeviceWidthScaleToiPhone6, 50*kDeviceWidthScaleToiPhone6);
}
@end