IOS ShareSDK实现分享——微信分享

 

IOS ShareSDK实现分享——微信分享


目录(?)[+]

最近领导让添加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文件。 导入头文件
[objc]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. #import <ShareSDK/ShareSDK.h>  
  2. #import "WXApi.h"  
实现代理

@interface GAShareUtil ()<WXApiDelegate>

@end

在- (BOOL)application: didFinishLaunchingWithOptions:方法中调用registerApp方法来初始化SDK并且初始化第三方平台
[objc]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. /** 
  2.      *  设置ShareSDK的appKey,如果尚未在ShareSDK官网注册过App,请移步到http://mob.com/login 登录后台进行应用注册 
  3.      *  在将生成的AppKey传入到此方法中。 
  4.      *  方法中的第二个第三个参数为需要连接社交平台SDK时触发, 
  5.      *  在此事件中写入连接代码。第四个参数则为配置本地社交平台时触发,根据返回的平台类型来配置平台信息。 
  6.      *  如果您使用的时服务端托管平台信息时,第二、四项参数可以传入nil,第三项参数则根据服务端托管平台来决定要连接的社交SDK。 
  7.      */  
  8.     [ShareSDK registerApp:@"iosv1101"  
  9.           activePlatforms:@[  
  10.                             @(SSDKPlatformTypeWechat)]  
  11.                  onImport:^(SSDKPlatformType platformType){  
  12.                      switch (platformType){  
  13.                          case SSDKPlatformTypeWechat:  
  14.                              [ShareSDKConnector connectWeChat:[WXApi class]delegate:self];  
  15.                              break;  
  16.                          case SSDKPlatformTypeQQ:  
  17.                              [ShareSDKConnector connectQQ:[QQApiInterface class] tencentOAuthClass:[TencentOAuth class]];  
  18.                              break;  
  19.                          case SSDKPlatformTypeSinaWeibo:  
  20.                              [ShareSDKConnector connectWeibo:[WeiboSDK class]];  
  21.                              break;  
  22.                          default:  
  23.                              break;  
  24.                      }  
  25.                  } onConfiguration:^(SSDKPlatformType platformType, NSMutableDictionary *appInfo){  
  26.                      switch (platformType){  
  27. //                        case SSDKPlatformTypeSinaWeibo:  
  28. //                             //设置新浪微博应用信息,其中authType设置为使用SSO+Web形式授权  
  29. //                            [appInfo SSDKSetupSinaWeiboByAppKey:@"3861325189"  
  30. //                                                      appSecret:@"1929f101f7929b8ed7a96f7ad4bedce1"  
  31. //                                                    redirectUri:@"http://www.ingenic.cn"  
  32. //                                                       authType:SSDKAuthTypeBoth];  
  33. //                             break;  
  34.                          case SSDKPlatformTypeWechat:  
  35.                              [appInfo SSDKSetupWeChatByAppId:@"wx4868b35061f87885"  
  36.                                  appSecret:@"558b231f64dce8fdf7a02d35386593bb"];  
  37.                 break;    
  38.              defaultbreak;   
  39. } }];  

然后打开下图位置,在URL Types中添加微信AppID   55BDA8FD-90BF-472B-999C-4C57680AB210

分享代码

1 简单分享 --无UI分享

[objc]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. /** 
  2.  *  简单分享 
  3.  */  
  4. - (void)simplyShare{  
  5.     /** 
  6.      * 在简单分享中,只要设置共有分享参数即可分享到任意的社交平台(前提是在AppDelegate中成功注册) 
  7.      **/  
  8.     __weak SHWeiXinViewController *theController = self;  
  9.     [self showLoadingView:YES];  
  10.     //创建分享参数  
  11.     NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];  
  12.     //可以是单张图片也可以是多张。  
  13.     NSArray* imageArray = @[[UIImage imageNamed:@"shareImg.png"]];  
  14.     if (imageArray) {  
  15.         [shareParams SSDKSetupShareParamsByText:@"分享内容"  
  16.                                          images:imageArray  
  17.                                             url:[NSURL URLWithString:@"http://www.mob.com"]  
  18.                                           title:@"分享标题"  
  19.                                            type:SSDKContentTypeAuto];  
  20.         //进行分享  
  21.         [ShareSDK share:SSDKPlatformSubTypeWechatSession  
  22.              parameters:shareParams  
  23. //        [ShareSDK share:SSDKPlatformTypeSinaWeibo  
  24. //             parameters:shareParams  
  25.          onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) {  
  26.              [theController showLoadingView:NO];  
  27.              [theController.wxtableView reloadData];  
  28.              switch (state) {//判断分享是否成功  
  29.                  case SSDKResponseStateSuccess:{  
  30.                      UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享成功"  
  31.                                                                          message:nil  
  32.                                                                         delegate:nil  
  33.                                                                cancelButtonTitle:@"确定"  
  34.                                                                otherButtonTitles:nil];  
  35.                      [alertView show];  
  36.                      break;  
  37.                  }  
  38.                  case SSDKResponseStateFail:{  
  39.                      UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享失败"  
  40.                                                                          message:[NSString stringWithFormat:@"%@", error]  
  41.                                                                         delegate:nil  
  42.                                                                cancelButtonTitle:@"确定"  
  43.                                                                otherButtonTitles:nil];  
  44.                      [alertView show];  
  45.                      break;  
  46.                  }  
  47.                  case SSDKResponseStateCancel:{  
  48.                      UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享已取消"  
  49.                                                                          message:nil  
  50.                                                                         delegate:nil  
  51.                                                                cancelButtonTitle:@"确定"  
  52.                                                                otherButtonTitles:nil];  
  53.                      [alertView show];  
  54.                      break;  
  55.                  }  
  56.                  default:  
  57.                      break;  
  58.              }  
  59.          }];  
  60.     }  
  61. }  

/**

 * 设置分享参数

 *  @param text     文本

 *  @param images   图片集合,传入参数可以为单张图片信息,也可以为一个NSArray,数组元素可以为UIImageNSString(图片路径)、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 分享菜单

显示分享菜单
[objc]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. - (void)showShareActionSheet:(UIView *)view{  
  2.     /** 
  3.      * 在简单分享中,只要设置共有分享参数即可分享到任意的社交平台 
  4.      **/  
  5.     __weak SHWeiXinViewController *theController = self;  
  6.     //1、创建分享参数(必要)  
  7.     NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];  
  8.     NSArray* imageArray = @[[UIImage imageNamed:@"shareImg.png"]];  
  9.     [shareParams SSDKSetupShareParamsByText:@"分享内容"  
  10.                                      images:imageArray  
  11.                                         url:[NSURL URLWithString:@"http://www.mob.com"]  
  12.                                       title:@"分享标题"  
  13.                                        type:SSDKContentTypeAuto];  
  14.       
  15.     //2、分享  
  16.     [ShareSDK showShareActionSheet:view  
  17.                              items:nil  
  18.                        shareParams:shareParams  
  19.                onShareStateChanged:^(SSDKResponseState state, SSDKPlatformType platformType, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error, BOOL end) {  
  20.                    switch (state) {  
  21.                        case SSDKResponseStateBegin:{  
  22.                            [theController showLoadingView:YES];  
  23.                            break;  
  24.                        }  
  25.                        case SSDKResponseStateSuccess:{  
  26.                            //Facebook Messenger、WhatsApp等平台捕获不到分享成功或失败的状态,最合适的方式就是对这些平台区别对待  
  27.                            if (platformType == SSDKPlatformTypeFacebookMessenger){  
  28.                                break;  
  29.                            }  
  30.                            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享成功"  
  31.                                                                                message:nil  
  32.                                                                               delegate:nil  
  33.                                                                      cancelButtonTitle:@"确定"  
  34.                                                                      otherButtonTitles:nil];  
  35.                            [alertView show];  
  36.                            break;  
  37.                        }  
  38.                        case SSDKResponseStateFail:{  
  39.                            if (platformType == SSDKPlatformTypeSMS && [error code] == 201){  
  40.                                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"分享失败"  
  41.                                                                                message:@"失败原因可能是:1、短信应用没有设置帐号;2、设备不支持短信应用;3、短信应用在iOS 7以上才能发送带附件的短信。"  
  42.                                                                               delegate:nil  
  43.                                                                      cancelButtonTitle:@"OK"  
  44.                                                                      otherButtonTitles:nil, nil nil];  
  45.                                [alert show];  
  46.                                break;  
  47.                            }else if(platformType == SSDKPlatformTypeMail && [error code] == 201){  
  48.                                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"分享失败"  
  49.                                                                                message:@"失败原因可能是:1、邮件应用没有设置帐号;2、设备不支持邮件应用;"  
  50.                                                                               delegate:nil  
  51.                                                                      cancelButtonTitle:@"OK"  
  52.                                                                      otherButtonTitles:nil, nil nil];  
  53.                                [alert show];  
  54.                                break;  
  55.                            }  
  56.                            else{  
  57.                                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"分享失败"  
  58.                                                                                message:[NSString stringWithFormat:@"%@",error]  
  59.                                                                               delegate:nil  
  60.                                                                      cancelButtonTitle:@"OK"  
  61.                                                                      otherButtonTitles:nil, nil nil];  
  62.                                [alert show];  
  63.                                break;  
  64.                            }  
  65.                            break;  
  66.                        }  
  67.                        case SSDKResponseStateCancel:{  
  68.                            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享已取消"  
  69.                                                                                message:nil  
  70.                                                                               delegate:nil  
  71.                                                                      cancelButtonTitle:@"确定"  
  72.                                                                      otherButtonTitles:nil];  
  73.                            [alertView show];  
  74.                            break;  
  75.                        }  
  76.                        default:  
  77.                            break;  
  78.                    }  
  79.                    if (state != SSDKResponseStateBegin){  
  80.                        [theController showLoadingView:NO];  
  81.                        [theController.wxtableView reloadData];  
  82.                    }                    
  83.                }];     
  84. }  


显示分享菜单的方法

[objc]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. + (SSUIShareActionSheetController *)showShareActionSheet:(UIView *)view  
  2.                                                    items:(NSArray *)items  
  3.                                              shareParams:(NSMutableDictionary *)shareParams  
  4.                                      onShareStateChanged:(SSUIShareStateChangedHandler)shareStateChangedHandler;  

当items参数为nil时,则会 显示已集成的平台列表,可以通过设置items只显示自己需要的平台。 其中SSDKPlatformTypeWechat默认包括微信好友,朋友圈和微信收藏。可以通过设置这三个参数可以让各别显示和隐藏。

SSDKPlatformSubTypeWechatSession,SSDKPlatformSubTypeWechatTimeline,SSDKPlatformSubTypeWechatFav


[objc]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. [ShareSDK showShareActionSheet:nil  
  2.                        //将要自定义顺序的平台传入items参数中  
  3.                        items:@[@(SSDKPlatformTypeWechatTimeLine)  
  4.                    @(SSDKPlatformTypeWechatSession)]   
  5.                  shareParams:shareParams   
  6.          onShareStateChanged:^(SSDKResponseState state, SSDKPlatformType platformType, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error, BOOL end) { .......}];  

效果图所示:

3 自定义分享菜单栏样式

[objc]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. // 弹出分享菜单需要导入的头文件  
  2. #import <ShareSDKUI/ShareSDK+SSUI.h>  
  3. // 自定义分享菜单栏需要导入的头文件  
  4. #import <ShareSDKUI/SSUIShareActionSheetStyle.h>  
  5.    
  6.     //1、创建分享参数(必要)  
  7.     NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];  
  8.     NSArray* imageArray = @[[UIImage imageNamed:@"shareImg.png"]];  
  9.    (注意:图片要在Xcode左边目录里面,名称必须要传正确,或者是本地沙盒中的文件,如果要分享网络图片,可以这样传iamge参数 images:@[@"http://mob.com/Assets/images/logo.png?v=20150320"])  
  10.     [shareParams SSDKSetupShareParamsByText:@"分享内容"  
  11.                                      images:imageArray  
  12.                                         url:[NSURL URLWithString:@"http://mob.com"]  
  13.                                       title:@"分享标题"  
  14.                                        type:SSDKContentTypeAuto];  
  15.    
  16.     // 设置分享菜单栏样式(非必要)  
  17.     // 设置分享菜单的背景颜色  
  18.     [SSUIShareActionSheetStyle setActionSheetBackgroundColor:[UIColor colorWithRed:249/255.0 green:0/255.0 blue:12/255.0 alpha:0.5]];  
  19.     // 设置分享菜单颜色  
  20.     [SSUIShareActionSheetStyle setActionSheetColor:[UIColor colorWithRed:21.0/255.0 green:21.0/255.0 blue:21.0/255.0 alpha:1.0]];  
  21.     // 设置分享菜单-取消按钮背景颜色  
  22.     [SSUIShareActionSheetStyle setCancelButtonBackgroundColor:[UIColor colorWithRed:21.0/255.0 green:21.0/255.0 blue:21.0/255.0 alpha:1.0]];  
  23.     // 设置分享菜单-取消按钮的文本颜色  
  24.     [SSUIShareActionSheetStyle setCancelButtonLabelColor:[UIColor yellowColor]];  
  25.     // 设置分享菜单-社交平台文本颜色  
  26.     [SSUIShareActionSheetStyle setItemNameColor:[UIColor redColor]];  
  27.     // 设置分享菜单-社交平台文本字体  
  28.     [SSUIShareActionSheetStyle setItemNameFont:[UIFont systemFontOfSize:11]];  
  29.    
  30.     //2、弹出ShareSDK分享菜单  
  31.     [ShareSDK showShareActionSheet:view  
  32.                              items:@[@(SSDKPlatformTypeWechatTimeLine)  
  33.                        @(SSDKPlatformTypeWechatSession)]  
  34.                        shareParams:shareParams  
  35.                onShareStateChanged:^(SSDKResponseState state, SSDKPlatformType platformType, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error, BOOL end) { ...... }  
效果图:

4 跳过分享的编辑界面

弹出分享菜单,直接点击菜单中的平台分享(跳过分享的编辑界面)。

[objc]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. //先构造分享参数:  
  2.  NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];  
  3.   [shareParams SSDKSetupShareParamsByText:@"分享内容"  
  4.                                          images:@[[UIImage imageNamed:@"shareImg.png"]]  
  5.                                             url:[NSURL URLWithString:@"http://mob.com"]  
  6.                                           title:@"分享标题"  
  7.                                            type:SSDKContentTypeAuto];  
  8.   //调用分享的方法  
  9.  SSUIShareActionSheetController *sheet = [ShareSDK showShareActionSheet:view  
  10.                                                                          items:nil  
  11.                                                                    shareParams:shareParams  
  12.                                                            onShareStateChanged:^(SSDKResponseState state, SSDKPlatformType platformType, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error, BOOL end) {  
  13.                                                                switch (state) {  
  14.                                                                    case SSDKResponseStateSuccess:  
  15.                                                                        NSLog(@"分享成功!");  
  16.                                                                        break;  
  17.                                                                    case SSDKResponseStateFail:  
  18.                                                                        NSLog(@"分享失败%@",error);  
  19.                                                                        break;  
  20.                                                                    case SSDKResponseStateCancel:  
  21.                                                                        NSLog(@"分享已取消");  
  22.                                                                        break;  
  23.                                                                    default:  
  24.                                                                        break;  
  25.                                                                }  
  26.                                                            }];  
  27.    //删除和添加平台示例  
  28.  [sheet.directSharePlatforms removeObject:@(SSDKPlatformTypeWechat)];  
  29. //(默认微信,QQ,QQ空间都是直接跳客户端分享,加了这个方法之后,可以跳分享编辑界面分享)  
  30.  [sheet.directSharePlatforms addObject:@(SSDKPlatformTypeSinaWeibo)];  
  31. //(其它平台默认会跳分享编辑界面,加了这个方法之后可以不跳分享编辑界面,直接点击分享菜单里的选项,直接分享)  

5 不显示分享平台显示分享编辑菜单

[objc]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. [ShareSDK showShareEditor:SSDKPlatformTypeWechat  
  2.                otherPlatformTypes:@[@(SSDKPlatformTypeSinaWeibo),@(SSDKPlatformTypeTencentWeibo)]  
  3.                       shareParams:shareParams  
  4.               onShareStateChanged:^(SSDKResponseState state, SSDKPlatformType platformType, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error, BOOL end)  
  5.          {}];  


显示分享编辑框,只有微信,不显示otherPlatformTypes中的其它选项。



6 自定义编辑框

导入

#import <ShareSDK/ShareSDK.h>

#import <ShareSDKUI/SSUIEditorViewStyle.h>

[objc]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];  
  2.         [shareParams SSDKSetupShareParamsByText:@"分享内容..."  
  3.                                          images:imageArray  
  4.                                             url:[NSURL URLWithString:@"http://mob.com"]  
  5.                                           title:@"分享标题"  
  6.                                            type:SSDKContentTypeImage];  
  7.         //自定义编辑区样式(非必要)  
  8.         //设置iPhone导航栏颜色  
  9.         [SSUIEditorViewStyle setiPhoneNavigationBarBackgroundColor:[UIColor redColor]];  
  10.         //设置编辑界面背景颜色  
  11.         [SSUIEditorViewStyle setContentViewBackgroundColor:[UIColor cyanColor]];  
  12.         //设置标题  
  13.         [SSUIEditorViewStyle setTitle:@"微信分享"];  
  14.         //设置取消按钮标签  
  15.         [SSUIEditorViewStyle setCancelButtonLabel:@"算了吧"];  
  16.         //设置取消按钮标签文本颜色  
  17.         [SSUIEditorViewStyle setCancelButtonLabelColor:[UIColor blackColor]];  
  18.           
  19.         [ShareSDK showShareEditor:SSDKPlatformTypeWechat  
  20.                otherPlatformTypes:@[@(SSDKPlatformTypeSinaWeibo),@(SSDKPlatformTypeTencentWeibo)]  
  21.                       shareParams:shareParams  
  22.               onShareStateChanged:^(SSDKResponseState state, SSDKPlatformType platformType, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error, BOOL end)  
  23.          {  
  24. }];  


7 隐藏“微信收藏”平台

方法有两种

1 在appDeleagete中修改,这样比较方便。

[objc]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. [ShareSDK registerApp:@"XXXXXXX"  
  2.             activePlatforms:@[  
  3.                             // 不要使用微信总平台进行初始化  
  4.                             //@(SSDKPlatformTypeWechat),  
  5.                             // 使用微信子平台进行初始化,即可  
  6.                             @(SSDKPlatformSubTypeWechatSession),  
  7.                             @(SSDKPlatformSubTypeWechatTimeline),  
  8.                             ]  
  9.             onImport:^(SSDKPlatformType platformType) {  
  10.    
  11.                      switch (platformType)  
  12.                      {  
  13.                          case SSDKPlatformTypeWechat:  
  14.                              [ShareSDKConnector connectWeChat:[WXApi class]];  
  15.                              break;  
  16.                         default:  
  17.                              break;  
  18.                      }  
  19.    
  20.                  }  
  21.             onConfiguration:^(SSDKPlatformType platformType, NSMutableDictionary *appInfo) {  
  22.    
  23.               switch (platformType)  
  24.               {  
  25.                    case SSDKPlatformTypeWechat:  
  26.                       [appInfo SSDKSetupWeChatByAppId:@"wx4868b35061f87885"  
  27.                                             appSecret:@"64020361b8ec4c99936c0e3999a9f249"];  
  28.                       break;  
  29.                    default:  
  30.                       break;  
  31.               }  
  32.           }];  

2 在某些使用的地方,将SSDKPlatformTypeWechat修改成微信子平台,去掉微信收藏。这个是针对个别地方隐藏。


8 平台内容定制

[objc]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. /** 
  2.      * 在定制平台内容分享中,除了设置共有的分享参数外,还可以为特定的社交平台进行内容定制, 
  3.      * 如:其他平台分享的内容为“分享内容”,但微信需要在原有的“分享内容”文字后面加入一条链接,则可以如下做法: 
  4.      **/  
  5.     __weak SHWeiXinViewController *theController = self;  
  6.     [theController showLoadingView:YES];  
  7.       
  8.     //创建分享参数  
  9.     NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];  
  10.       
  11.     NSArray* imageArray = @[[UIImage imageNamed:@"shareImg.png"]];  
  12.       
  13.     if (imageArray) {  
  14.           
  15.         [shareParams SSDKSetupShareParamsByText:@"分享内容"  
  16.                                          images:imageArray  
  17.                                             url:[NSURL URLWithString:@"http://mob.com"]  
  18.                                           title:@"分享标题"  
  19.                                            type:SSDKContentTypeImage];  
  20.           
  21.         [shareParams SSDKSetupWeChatParamsByText:@"分享内容 http://mob.com"  
  22.                                            title:@"分享标题"  
  23.                                              url:[NSURL URLWithString:@"http://mob.com"]  
  24.                                       thumbImage:[UIImage imageNamed:@"shareImg.png"]  
  25.                                            image:[UIImage imageNamed:@"shareImg.png"]  
  26.                                     musicFileURL:nil  
  27.                                          extInfo:nil  
  28.                                         fileData:nil  
  29.                                     emoticonData:nil  
  30.                              sourceFileExtension:nil  
  31.                                   sourceFileData:nil  
  32.                                             type:SSDKContentTypeAuto  
  33.                               forPlatformSubType:SSDKPlatformSubTypeWechatSession];  
  34.           
  35.           
  36.         //进行分享  
  37.         [ShareSDK share:SSDKPlatformTypeWechat  
  38.              parameters:shareParams  
  39.          onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) {  
  40. }];  


9 分享本地文件(mp3、mp4、docx、pdf等)


[objc]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];  
  2.     UIImage *imageThumb = [UIImage imageNamed:@"Icon.png"];  
  3.     NSString *filePath = [[NSBundle mainBundle] pathForResource:@"cattest" ofType:@"mp4"];  
  4.     [shareParams SSDKSetupWeChatParamsByText:@"视频分享。。。"  
  5.                                        title:@"视频分享"  
  6.                                          url:nil  
  7.                                   thumbImage:imageThumb  
  8.                                        image:imageThumb  
  9.                                 musicFileURL:nil  
  10.                                      extInfo:nil  
  11.                                     fileData:nil  
  12.                                 emoticonData:nil  
  13.                          sourceFileExtension:@"mp4"  
  14.                               sourceFileData:filePath  
  15.                                         type:SSDKContentTypeFile  
  16.                           forPlatformSubType:SSDKPlatformSubTypeWechatSession];  
  17.       
  18.     //进行分享  
  19.     [ShareSDK share:SSDKPlatformSubTypeWechatSession  
  20.          parameters:shareParams  
  21.      onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) {  
  22. }];  

分享文件时:

 *  设置typeSSDKContentTypeFile(例如.mp3.mp4.pdf.docx的分享),设置titlesourceFileExtensionsourceFileData,以及thumbImage参数,如果尚未设置thumbImage则会从image参数中读取图片并对图片进行缩放操作参数

 */

注意(分享失败的两个坑):文件大小不能超过10M,thumbImage图片大小不能超过32k,最好是宽高相等。


10 摇一摇分享

导包

#import <ShareSDK/ShareSDK.h>

#import <ShareSDKExtension/SSEShareHelper.h>

[objc]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. /** 
  2.          * 使用ShareSDKExtension插件可以实现摇一摇分享 
  3.          * 通过使用SSEShareHelper可以调用开启/结束摇一摇分享方法,在方法的onWillShareHandler参数中可以处理摇一摇后的分享处理操作。 
  4.          * 
  5.          * 小技巧: 
  6.          * 当取得摇一摇事件通知后,如果shareHandler入口不满足分享需求(如需要弹出分享菜单而不是直接分享),可以不调用shareHandler进行分享,而是在block中写入自定义的分享操作。 
  7.          * 这样的话摇一摇分享接口实质只充当捕获摇一摇通知的功能。 
  8.          **/  
  9.           
  10.         __weak SHWeiXinViewController *theController = self;  
  11.           
  12.         //开启摇一摇分享功能  
  13.         [SSEShareHelper beginShakeShare:nil  
  14.                               onEndSake:nil  
  15.                      onWillShareHandler:^(SSEShareHandler shareHandler){  
  16.              NSLog(@"======开始分享");  
  17.              //构造分享参数  
  18.              NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];  
  19.              NSArray* imageArray = @[[UIImage imageNamed:@"shareImg.png"]];  
  20.              if (imageArray){  
  21.                  [shareParams SSDKSetupShareParamsByText:@"摇着摇着就可以分享出去了,使用ShareSDK分享就是便捷方便。"  
  22.                                                   images:imageArray  
  23.                                                      url:nil  
  24.                                                    title:nil  
  25.                                                     type:SSDKContentTypeImage];  
  26.                  //显示等待界面  
  27.                  [theController showLoadingView:YES];  
  28.                  //回调分享  
  29.                  if (shareHandler){  
  30.                      shareHandler (SSDKPlatformTypeWechat, shareParams);  
  31.                  }  
  32.              }  
  33.          }onStateChanged:^(SSDKResponseState state,NSDictionary *userData,  
  34.                            SSDKContentEntity *contentEntity, NSError *error) {  
  35. }];  

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;

751FEFB8-86A2-45EC-8F61-A1A5081C5465

(2)http://wiki.mob.com/ios9-对sharesdk的影响(适配ios-9必读)/


demo下载地址

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页