- 资料申请 微信key shareSDK key的申请可以参考 上一篇第三方登录申请流程
pod ‘mob_sharesdk’
pod ‘mob_sharesdk/ShareSDKUI’
pod ‘mob_sharesdk/ShareSDKPlatforms/WeChat’
使用配置文件分享模块(非必需)
pod ‘mob_sharesdk/ShareSDKConfigFile’
pod ‘mob_sharesdk/ShareSDKExtension’
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self setupShareSDK];
}
/** 设置ShareSDK */
+ (void)setupShareSDK{
[ShareSDK registPlatforms:^(SSDKRegister *platformsRegister) {
//微信
[platformsRegister setupWeChatWithAppId:WeChat_AppID appSecret:WeChat_Appsecret];
}];
}
-
初始化sdk后,就要根据请求后台返回需要分享的一些参数,再将后台返回的参数根据需求文档填入shareSDK对应的api接口
不同分享类型有的是只是分享一张图片,有的有文字和图片链接这取决于下面的type:SSDKContentTypeImage还是type:SSDKContentTypeAuto或者其他类型。详细参考文档 -
需导入 #import <ShareSDK/ShareSDK.h>
#import <ShareSDKUI/ShareSDK+SSUI.h> -
构造分享方法
#pragma mark -- 微信分享
+(void)shareWithTitle:(NSString *)title withContent:(NSString *)content withImageForPath:(UIImage *)imagePath withImageForURL:(NSString *)imageURL withShareLinkURL:(NSString *)linkURL withSender:(UIButton *)sender{
//1、构造分享内容
//1.1、要分享的图片(以下分别是网络图片和本地图片的生成方式的示例)
//===== imageURL 图片URL string
NSArray* imageArray;
//1、创建分享参数
if (imageURL) {
imageArray = @[imageURL];
}else {
imageArray = @[imagePath];
}
//#ifdef DEBUG
// NSDictionary * param = [MDCommon urlParameterForJsonWithURL:linkURL];
// MDLog(@"分享1%@",param);
// MDLog(@"分享2%@",[MDCommon decodeString:param[@"state"]]);
// MDLog(@"分享3%@",[MDCommon decodeString:linkURL]);
// linkURL = [MDCommon decodeString:[MDCommon decodeString:param[@"state"]]];
//#endif
//
//(注意:图片必须要在Xcode左边目录里面,名称必须要传正确,如果要分享网络图片,可以这样传iamge参数 images:@[@"http://mob.com/Assets/images/logo.png?v=20150320"])
if (imageArray) {
NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
[shareParams SSDKSetupShareParamsByText:content
images:imageArray
url:[NSURL URLWithString:linkURL]
title:title
type:SSDKContentTypeAuto];
//有的平台要客户端分享需要加此方法,例如微博
[shareParams SSDKEnableUseClientShare];
//2、分享(可以弹出我们的分享菜单和编辑界面)
[ShareSDK showShareActionSheet:nil //要显示菜单的视图, iPad版中此参数作为弹出菜单的参照视图,只有传这个才可以弹出我们的分享菜单,可以传分享的按钮对象或者自己创建小的view 对象,iPhone可以传nil不会影响
items:nil
shareParams:shareParams
onShareStateChanged:^(SSDKResponseState state, SSDKPlatformType platformType, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error, BOOL end) {
MDLog(@"ss%lu",(unsigned long)state);
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]
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil, nil];
[alert show];
break;
}
default:
break;
}
}
];
}
}
+(void)shareWithImageForPath:(UIImage *)imagePath withImage:(UIImage *)image withSender:(UIButton *)sender {
NSArray* imageArray;
//1、创建分享参数
if (imagePath) {
imageArray = @[imagePath];
}else {
imageArray = @[image];
}
//(注意:图片必须要在Xcode左边目录里面,名称必须要传正确,如果要分享网络图片,可以这样传iamge参数 images:@[@"http://mob.com/Assets/images/logo.png?v=20150320"])
if (imageArray) {
NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
[shareParams SSDKSetupShareParamsByText:nil
images:imageArray
url:nil
title:nil
type:SSDKContentTypeAuto];
//有的平台要客户端分享需要加此方法,例如微博
[shareParams SSDKEnableUseClientShare];
//2、分享(可以弹出我们的分享菜单和编辑界面)
[ShareSDK showShareActionSheet:nil //要显示菜单的视图, iPad版中此参数作为弹出菜单的参照视图,只有传这个才可以弹出我们的分享菜单,可以传分享的按钮对象或者自己创建小的view 对象,iPhone可以传nil不会影响
items:nil
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]
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil, nil];
[alert show];
break;
}
default:
break;
}
}
];
}
}
+(void)shareWithImageURL:(NSString *)imageURL withSender:(UIButton *)sender withShareState:(void (^)(BOOL))SuccessShareState {
NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
[shareParams SSDKSetupShareParamsByText:nil
images:@[imageURL]
url:nil
title:nil
type:SSDKContentTypeImage];
//有的平台要客户端分享需要加此方法,例如微博
[shareParams SSDKEnableUseClientShare];
//2、分享(可以弹出我们的分享菜单和编辑界面)
[ShareSDK showShareActionSheet:nil //要显示菜单的视图, iPad版中此参数作为弹出菜单的参照视图,只有传这个才可以弹出我们的分享菜单,可以传分享的按钮对象或者自己创建小的view 对象,iPhone可以传nil不会影响
items:nil
shareParams:shareParams
onShareStateChanged:^(SSDKResponseState state, SSDKPlatformType platformType, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error, BOOL end) {
MDLog(@"ss%lu",(unsigned long)state);
switch (state) {
case SSDKResponseStateBegin:
{
SuccessShareState(YES);
break;
}
case SSDKResponseStateCancel:
{
SuccessShareState(NO);
break;
}
default:
break;
}
}
];
}
- 下面就是在需要分享的时候调用
[selff GET_WechatSharerequestWithContent:(NSDictionary *dic) {
ResponseModel * response = [ResponseModel setObjectWithDictonary:dic];
if (response.isRequestSuccess) {
ShareContentModel * shareModel = [ShareContentModel setObjectWithDictonary:response.result];
[self shareWithTitle:shareModel.title withContent:shareModel.content withImageForPath:nil withImageForURL:[img01PathURL stringByAppendingPathComponent:shareModel.picAddr] withShareLinkURL:shareModel.weChatUrl withSender:self.rightButton];
}
} orFailureBlock:^{
}];