­

iOS微信之简单文本分享(ShareSDK)

  • 2019 年 11 月 7 日
  • 筆記

先上效果图

效果图.png

前言

开发工具:XCode 7.3.1 ShareSDK版本:V3.1.1

一.注册ShareSDK帐号并创建一个APP

ShareSDK官网:http://www.mob.com/#/

官网.png

创建一个APP.png

得到App Key,App Secret(后面的代码整合中会用到)

获取AppKey,AppSecret.png

二.从微信开放平台获取AppID,AppSecret

参考博文: http://www.jianshu.com/p/839dc30f2250 http://www.jianshu.com/p/c9b688fdb961

三.下载SDK

下载地址: http://www.mob.com/#/downloadDetail/ShareSDK/ios

SDK下载.png

SDK配置.png

SDK下载完成后,得到如下的文件目录结构

SDK文件目录.png

四.代码整合

1.导入SDK

导入SDK.png

2.设置支持HTTP请求

支持HTTP请求.png

参考博文:http://www.jianshu.com/p/5935dff47e4f

3.设置sheme白名单

设置Scheme白名单.png

参考博文:http://www.jianshu.com/p/f974f4cbba18

4.关闭bitcode

禁用bitcode1.png

禁用bitcode2.png

5.设置URL

设置URL.png

6.设置Build phases

设置Build Phases.png

7.核心代码

AppDelegate.m – (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

// 从sharesdk平台获取  NSString *sharesdkAppKey = @"";    #pragma mark 隐藏“微信收藏”平台  // 不要使用微信总平台进行初始化  // @(SSDKPlatformTypeWechat),  // 使用微信子平台进行初始化,即可  // @(SSDKPlatformSubTypeWechatSession),  // @(SSDKPlatformSubTypeWechatTimeline),    [ShareSDK registerApp:sharesdkAppKey        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:               // 从微信开放平台获取 AppID,AppSecret               [appInfo SSDKSetupWeChatByAppId:@""                                     appSecret:@""];               break;           default:               break;       }   }];  return YES;  }    -(void)onResp:(BaseResp *)resp  {  NSLog(@"The response of wechat.");  }

分享代码

-(IBAction)clickShareButton:(id)sender{  //1、创建分享参数  NSArray* imageArray = @[[UIImage imageNamed:@"shareImg.png"]];    if (imageArray) {        NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];      [shareParams SSDKSetupShareParamsByText:@"分享内容"                                       images:imageArray                                          url:[NSURL URLWithString:@"http://mob.com"]                                        title:@"分享标题"                                         type:SSDKContentTypeAuto];      //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;                     }                 }       ];   }  }

源码地址 https://github.com/andli0626/sharesdk_wxshare_for_ios.git

万事开头难,完成一个简单文本分享,后面的其他API的使用就可以参考官方文档,一步步实践了。

参考资料:

ShareSDK 官方文档  http://wiki.mob.com/ios%E7%AE%80%E6%B4%81%E7%89%88%E5%BF%AB%E9%80%9F%E9%9B%86%E6%88%90/  微信开放平台官方文档  https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=1417694084&token=2fd5d537adee029114cb21e9175ac913d51ff274&lang=zh_CN

image