微信分享

  • 资料申请 微信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:^{
            
        }];



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值