最近领导让添加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 (需要的类库) :
----- MOBFoundation.framework:基础功能框架。(必要)
----- ShareSDK.bundle:ShareSDK资源文件。(必要)
----- ShareSDKConnector.framework:用于ShareSDK框架与外部框架连接的代理框架插件。(使用第三方SDK时必要。)
(b) Optional (可选类库):
----- ShareSDKExtension.framework:对ShareSDK功能的扩展框架插件。
(主要提供第三方平台登录、 一键分享、截屏分享、摇一摇分享等相关功能。需要使用以上功能时必要。)
----- ShareSDKInterfaceAdapter.framework:ShareSDK v2.x对简洁版的接口兼容包。
----- ShareSDKUI.bundle:分享菜单栏和分享编辑页面资源包。(如果自定义这些UI可直接移除)
----- ShareSDKUI.framework:分享菜单栏和分享编辑页面。(如果自定义这些UI可直接移除)
(c) PlatformSDK 第三方平台SDK。(不需要的平台的SDK可直接移除)
其中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 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 SSDKPlatformTypeWechat:
- [appInfo SSDKSetupWeChatByAppId:@"wx4868b35061f87885"
- appSecret:@"558b231f64dce8fdf7a02d35386593bb"];
- break;
- default: break;
- } }];
然后打开下图位置,在URL Types中添加微信AppID
分享代码
1 简单分享 --无UI分享
-
-
-
-
- - (void)simplyShare{
-
-
-
- __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
-
-
- 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;
-
- NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
- NSArray* imageArray = @[[UIImage imageNamed:@"shareImg.png"]];
- [shareParams SSDKSetupShareParamsByText:@"分享内容"
- images:imageArray
- url:[NSURL URLWithString:@"http://www.mob.com"]
- title:@"分享标题"
- type:SSDKContentTypeAuto];
-
-
- [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:{
-
- 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 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 nil];
- [alert show];
- break;
- }
- else{
- UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"分享失败"
- message:[NSString stringWithFormat:@"%@",error]
- delegate:nil
- cancelButtonTitle:@"OK"
- otherButtonTitles:nil, 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:@[@(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>
-
-
- 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]];
-
-
- [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)];
-
- [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];
-
-
- [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:@[
-
-
-
- @(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>
-
-
-
-
-
-
-
-
-
- __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必读)/
demo下载地址