最近领导让添加ios 微信分享视频的功能,之前做的只有微信分享本地图片的功能。
查看官网并没有找到答案,后来在其官网论坛中http://bbs.mob.com/thread-20938-1-1.html 这里面发现新版的sdk(v3.2.1)中才添加的微信分享本地文件(mp3、mp4、docx、pdf等)的功能。可以支持就好办了。。
使用集成好的第三方框架可以节省许多代码量。
注意:申请shareSdk的apply,还有各个平台的appkey和appsecert(这真是非常麻烦的事情啊)
集成SDK
1 下载ShareSDK 简洁版(v3.2.1)
目录如下所示:
目录结构:
(1)ShareSDk.framework:核心静态库。(必要)
(2) Support 文件夹。其中包含三个文件夹:
(a) Required (需要的类库) :
其中PlatformSDK文件夹下用来添加各个平台分享的sdk,可以根据需求下载。
2 将sdk导入工程
选中ShareSDK文件夹向工程中拖拽,拖到工程中后弹出以下对话框,勾选"Copy items into destination group's folder(if needed)"
,并点击“Finish“按钮, 如图

注意:请务必在上述步骤中选择“Create groups for any added folders”
单选按钮组。如果你选择“Create folder references for any added folders”
,一个蓝色的文件夹引用将被添加到项目并且将无法找到它的资源。
3 添加依赖库
如下图:3是Build phases。 4 是Link Binary With Libraries。

-
必须添加的依赖库如下(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 images 图片集合,传入参数可以为单张图片信息,也可以为一个NSArray,数组元素可以为UIImage、NSString(图片路径)、NSURL(图片路径)、SSDKImage。如: @"http://www.mob.com/images/logo_black.png"或@[@"http://www.mob.com/images/logo_black.png"]
* @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必读)/