本篇的主题有三个:

1、封装思想的介绍

2、我的封装代码

3、我在封装sharesdk(采用的是简洁版本)分享功能是碰到的问题,以及解决方法。

PS:其实这个我之前封装过一次,不过最近在重构项目时发现,当时封装的是如此的垃圾,所以在这里再来一次。欢迎大家批评纠错。

封装思想

因为这次封装的第三方SDK的功能,所以我采用延展的方式来进行封装。这样有以下两种好处:

1、 这样将第三方功能给模块化,在项目中方便查找和修改。

2、 很多第三方功能都是需要在appdelegae初始化,采用category只需在扩展的类中申明一个public方法,将初始化的代码放在相应的分类public中即可。最         后只需在appdelegate调用相应的功能模块初始化方法即可。

下面两张图,是我的延展类的形式和我在项目中封装两个第三方功能后,Appdelegate中的代码情况。

ShareSDK功能的封装

AppDelegate+ShareSDk.h

  1. //
  2. // AppDelegate+ShareSDk.h
  3. // CDL_optimize
  4. //
  5. // Created by 王立广 on 15/9/11.
  6. // Copyright (c) 2015年 王立广. All rights reserved.
  7. //
  8.  
  9. #import "AppDelegate.h"
  10.  
  11. @interface AppDelegate (ShareSDk)
  12.  
  13. /**
  14. * shareSDK分享
  15. */
  16. - (void)addShareSDKWithapplication:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;
  17.  
  18. /**
  19. * 定制平台分享内容分享
  20. */
  21. - (void)platShareView:(UIView *)view WithShareContent:(NSString *)shareContent WithShareUrlImg:(NSString *)shareUrlImg WithShareTitle:(NSString *)shareTitle WithShareId:(NSNumber *)shareId WithShareType:(kShareType *)shareType;
  22.  
  23. @end

AppDelegate+shareSDK.m

  1. // AppDelegate+ShareSDk.m
  2. // CDL_optimize
  3. //
  4. // Created by 王立广 on 15/9/11.
  5. // Copyright (c) 2015年 王立广. All rights reserved.
  6. //
  7. #import "AppDelegate+ShareSDk.h"
  8. #import <ShareSDK/ShareSDK.h>
  9. #import <ShareSDKExtension/SSEShareHelper.h>
  10. #import <ShareSDKUI/ShareSDK+SSUI.h>
  11. #import <ShareSDKUI/SSUIShareActionSheetStyle.h>
  12. #import <ShareSDKUI/SSUIShareActionSheetCustomItem.h>
  13. #import <ShareSDK/ShareSDK+Base.h>
  14. #import <ShareSDK/ShareSDK.h>
  15. #import <TencentOpenAPI/QQApiInterface.h>
  16. #import <TencentOpenAPI/TencentOAuth.h>
  17. #import "WXApi.h"
  18. #import "WeiboSDK.h"
  19. #import <ShareSDKConnector/ShareSDKConnector.h>
  20. //新浪微博
  21. #define kSinaWeiboAPPKey @"*********"
  22. #define kSinaWeiboAPPSecret @"************"
  23. //腾讯微博
  24. #define kTencentWeiboAPPKey @"*********"
  25. #define kTencentWeiboAPPSecret @"**********"
  26. //QQ
  27. #define kQQAPPId @"**********"
  28. #define kQQAPPKey @"**********"
  29. //微信
  30. #define kWechatAPPId @"*************"
  31. #define kWechatAPPSecret @"************"
  32. //下面这个枚举用来判断分享哪个模块,建议放在pch文件中
  33. //typedef enum
  34. //{
  35. // shareDartbar,//镖吧分享
  36. // shareInfo, //资讯分享
  37. //
  38. //}kShareType;
  39. @implementation AppDelegate (ShareSDk)
  40. - (void)addShareSDKWithapplication:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  41. {
  42. //初始化配置
  43. [self shareInit];
  44. }
  45. #pragma mark 分享平台初始化
  46. - (void)shareInit
  47. {
  48. NSArray *platformArray = [NSArray array];
  49. platformArray = @[@(SSDKPlatformTypeSinaWeibo),
  50. @(SSDKPlatformTypeTencentWeibo),
  51. @(SSDKPlatformTypeWechat),
  52. @(SSDKPlatformTypeQQ),
  53. ];
  54. /**
  55. * 构造分享平台
  56. *
  57. * @param platformType 分享平台
  58. *
  59. * @param onImport 此时如果要分享到一些客户端这个block块必须要填。
  60. *
  61. * @param onConfiguration appkey的相关配置
  62. */
  63. [ShareSDK registerApp:@"712aaee4e6ee" activePlatforms:platformArray
  64. onImport:^(SSDKPlatformType platformType) {
  65. switch (platformType)
  66. {
  67. case SSDKPlatformTypeWechat:
  68. [ShareSDKConnector connectWeChat:[WXApi class]];
  69. break;
  70. case SSDKPlatformTypeQQ:
  71. [ShareSDKConnector connectQQ:[QQApiInterface class] tencentOAuthClass:[TencentOAuth class]];
  72. break;
  73. default:
  74. break;
  75. }
  76. }
  77. onConfiguration:^(SSDKPlatformType platformType, NSMutableDictionary *appInfo) {
  78. switch(platformType)
  79. {
  80. case SSDKPlatformTypeSinaWeibo:
  81. //设置新浪微博应用信息,其中authType设置为使用SSO+web形式授权
  82. [appInfo SSDKSetupSinaWeiboByAppKey:kSinaWeiboAPPKey appSecret:kSinaWeiboAPPSecret redirectUri:@"http://www.sharesdk.cn" authType:SSDKAuthTypeBoth];
  83. break;
  84. case SSDKPlatformTypeTencentWeibo:
  85. //设置腾讯微博应用信息,其中authType只能使用web形式授权
  86. [appInfo SSDKSetupTencentWeiboByAppKey:kTencentWeiboAPPKey appSecret:kTencentWeiboAPPSecret redirectUri:@"http://www.sharesdk.cn"];
  87. break;
  88. case SSDKPlatformTypeQQ:
  89. //QQ平台
  90. [appInfo SSDKSetupQQByAppId:kQQAPPId appKey:kQQAPPKey authType:SSDKAuthTypeBoth];
  91. break;
  92. case SSDKPlatformTypeWechat:
  93. //微信平台
  94. [appInfo SSDKSetupWeChatByAppId:kWechatAPPId appSecret:kWechatAPPSecret];
  95. break;
  96. }
  97. }];
  98. }
  99. - (void)platShareView:(UIView *)view WithShareContent:(NSString *)shareContent WithShareUrlImg:(NSString *)shareUrlImg WithShareTitle:(NSString *)shareTitle WithShareId:(NSNumber *)shareId WithShareType:(kShareType *)shareType
  100. {
  101. NSString *shareUrl = nil;
  102. if(shareType == shareInfo){
  103. shareUrl = kInfoShareRequest(shareId);
  104. }else{
  105. shareUrl = kDartBarShareRequest(shareId);
  106. }
  107. //创建分享参数
  108. NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
  109. #pragma mark 公共分享参数
  110. // [shareParams SSDKSetupShareParamsByText:@"分享内容"
  111. // images:imageArray
  112. // url:[NSURL URLWithString:@"http://mob.com"]
  113. // title:@"分享标题"
  114. // type:SSDKContentTypeImage];
  115. #pragma mark 平台定制分享参数
  116. //新浪微博
  117. [shareParams SSDKSetupSinaWeiboShareParamsByText:[NSString stringWithFormat:@"%@ %@",shareContent,shareUrl] title:shareTitle image:kLoadNetImage(shareUrlImg) url:nil latitude: longitude: objectID:nil type:SSDKContentTypeAuto];
  118. //腾讯微博
  119. [shareParams SSDKSetupTencentWeiboShareParamsByText:[NSString stringWithFormat:@"%@ %@",shareContent,shareUrl] images:kLoadNetImage(shareUrlImg) latitude: longitude: type:SSDKContentTypeText];
  120. //QQ空间
  121. [shareParams SSDKSetupQQParamsByText:nil title:shareTitle url:[NSURL URLWithString:shareUrl] thumbImage:kLoadNetImage(shareUrlImg) image:kLoadNetImage(shareUrlImg) type:SSDKContentTypeWebPage forPlatformSubType:SSDKPlatformSubTypeQZone];
  122. //QQ好友
  123. [shareParams SSDKSetupQQParamsByText:nil title:shareTitle url:[NSURL URLWithString:shareUrl] thumbImage:kLoadNetImage(shareUrlImg) image:kLoadNetImage(shareUrlImg) type:SSDKContentTypeWebPage forPlatformSubType:SSDKPlatformSubTypeQQFriend];
  124. //微信收藏
  125. [shareParams SSDKSetupWeChatParamsByText:nil title:shareTitle url:[NSURL URLWithString:shareUrl] thumbImage:kLoadNetImage(shareUrlImg) image:nil musicFileURL:nil extInfo:nil fileData:nil emoticonData:kLoadNetImage(shareUrlImg) type:SSDKContentTypeWebPage forPlatformSubType:SSDKPlatformSubTypeWechatFav];
  126. //微信好友
  127. [shareParams SSDKSetupWeChatParamsByText:nil title:shareTitle url:[NSURL URLWithString:shareUrl] thumbImage:kLoadNetImage(shareUrlImg) image:kLoadNetImage(shareUrlImg) musicFileURL:nil extInfo:nil fileData:nil emoticonData:nil type:SSDKContentTypeWebPage forPlatformSubType:SSDKPlatformSubTypeWechatSession];
  128. //微信朋友圈
  129. [shareParams SSDKSetupWeChatParamsByText:nil title:shareTitle url:[NSURL URLWithString:shareUrl] thumbImage:kLoadNetImage(shareUrlImg) image:kLoadNetImage(shareUrlImg) musicFileURL:nil extInfo:nil fileData:nil emoticonData:nil type:SSDKContentTypeWebPage forPlatformSubType:SSDKPlatformSubTypeWechatTimeline];
  130. #pragma mark 不跳过编辑界面的分享框
  131. // [ShareSDK showShareActionSheet:view items:[ShareSDK activePlatforms] shareParams:shareParams onShareStateChanged:^(SSDKResponseState state, SSDKPlatformType platformType, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error, BOOL end) {
  132. //
  133. // switch (state) {
  134. // case SSDKResponseStateSuccess:
  135. // {
  136. // UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享成功"
  137. // message:nil
  138. // delegate:nil
  139. // cancelButtonTitle:@"确定"
  140. // otherButtonTitles:nil];
  141. // [alertView show];
  142. // break;
  143. // }
  144. // case SSDKResponseStateFail:
  145. // {
  146. // UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享失败"
  147. // message:[NSString stringWithFormat:@"%@", error]
  148. // delegate:nil
  149. // cancelButtonTitle:@"确定"
  150. // otherButtonTitles:nil];
  151. // [alertView show];
  152. // break;
  153. // }
  154. // case SSDKResponseStateCancel:
  155. // {
  156. // break;
  157. // }
  158. // default:
  159. // break;
  160. // }
  161. // }];
  162. #pragma mark 设置跳过分享编辑页面,直接分享的平台。
  163. SSUIShareActionSheetController *sheet = [ShareSDK showShareActionSheet:view items:nil shareParams:shareParams onShareStateChanged:^(SSDKResponseState state, SSDKPlatformType platformType, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error, BOOL end) {
  164. switch (state)
  165. {
  166. case SSDKResponseStateSuccess:
  167. {
  168. UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享成功"message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
  169. [alertView show];
  170. break;
  171. }
  172. case SSDKResponseStateFail:
  173. {
  174. UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"分享失败"
  175. message:[NSString stringWithFormat:@"%@",error] delegate:nil cancelButtonTitle:@"确定"otherButtonTitles:nil];
  176. [alertView show];
  177. break;
  178. }
  179. case SSDKResponseStateCancel:
  180. {
  181. break;
  182. }
  183. default:
  184. break;
  185. }
  186. }];
  187. //删除和添加平台示例
  188. [sheet.directSharePlatforms addObject:@(SSDKPlatformTypeSinaWeibo)];
  189. [sheet.directSharePlatforms addObject:@(SSDKPlatformTypeTencentWeibo)];
  190. }
  191. @end

PS:在代码里注释我都加上去了,个人感觉算是十分详细了,如果有问题,可以留下你的留言。

封装过程中碰到的问题以及解决方法

1、面板上一直显示不出来,相应的分享平台

在shareSDK的初始化方法中,有个onImport参数,如果分享的到app里,要传递这个参     数,要不然,在面板中不会显示这些平台的

2、新浪微博分享时,怎么才能将shareSdk给的界面里填的分享内容(这个界面默认是),分享到新浪微博里

只有分享的参数是公共的时候,在编辑页面修改的内容才会显示在分享的平台上。如果是给各个平台定制分享内容的话,在编辑页面修改的内容不会显示在分享的平台上,另外此时需要隐藏编辑界面,在代码中已注释。

3、在平台分享时我选择的是自动匹配分享类型,但我分享的内容没有图片时却分享不成功

选在分享类型的时候,能确定属于哪个类型,就选择哪个,如果实在确定不了就选自动

如果分享的内容有url的时候,一般选择SSDKContentTypeWebPage类型,如果不行在选自动。

4、分享到腾讯微博、新浪微博,要添加连接时,在内容后面填上链接。

封装ShareSDK中的分享功能封以及对类似第三方功能封装的心得【原创】的更多相关文章

  1. ShareSDK中微信分享错误总结

    项目中用到微信分享,可向好友或朋友圈分享链接时,分享人可以打开网站,查看消息者却始终不能打开网站.试了N种方法,重写了N次分享模块,均没办法解决. 在无意中查看分享链接时发现,朋友圈里分享后,原始链接 ...

  2. 在Android Studio中使用shareSDK进行社会化分享(图文教程)

    [声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...

  3. React Native(十五)——RN中的分享功能

    终于,终于,可以总结自己使用RN时的分享功能了-- 为什么呢?且听我慢慢道来吧: 从刚开始接触React Native(2017年9月中旬)就着手于分享功能,直到自己参与公司的rn项目开发中,再到现在 ...

  4. 自己封装的一个JS分享组件

    因为工作的需求之前也封装过一个JS分享插件,集成了我们公司常用的几个分享平台. 但是总感觉之前的结构上很不理想,样式,行为揉成一起,心里想的做的完美,实际上总是很多的偏差,所以这次我对其进行了改版. ...

  5. TOP100summit:【分享实录-封宇】58到家多端消息整合之路

    本篇文章内容来自2016年TOP100summit 58到家架构师封宇的案例分享. 编辑:Cynthia 2017年11月9-12日北京国家会议中心第六届TOP100summit,留言评论有机会获得免 ...

  6. Android 中实现分享和第三方登陆---以新浪微博为例

    第三方登陆和分享功能在目前大部分APP中都有,分享功能可以将自己觉得有意义的东西分享给身边的朋友,而第三方登陆可以借助已经有巨大用户基础的平台(如QQ和新浪微博)的账号,让用户在使用自己APP的时候不 ...

  7. 分享公司DAO层动态SQL的一些封装

    主题 公司在DAO层使用的框架是Spring Data JPA,这个框架很好用,基本不需要自己写SQL或者HQL就能完成大部分事情,但是偶尔有一些复杂的查询还是需要自己手写原生的Native SQL或 ...

  8. 手机QQ内置网页,微信内置网页中进行分享到QQ和微信的操作

    微信内的网页分享: API内容详见微信开发文档  https://mp.weixin.qq.com/wiki 这里需要注意的是:调用微信API的时候修改的是微信内网页右上角三个点那里打开后,选择分享之 ...

  9. ionic单页面应用中微信分享的问题总结

    首先说一下 ionic 是单页面应用,也就是说整个项目就有一个index.html, 那么问题就就来了, 如果我们不同的页面要分享给大家的是不同的链接和图片,应该怎么去做呢? 这就是我们今天要总结的东 ...

随机推荐

  1. 使用Maven下载jar包

    有些开源项目不直接提供jar包的下载,而是建议使用Maven下载,以开源库hipster(https://github.com/citiususc/hipster,http://www.hipster ...

  2. java前后端加密(转载)

    最近做一个项目的安全渗透测评,测评人员发来一份测试报告,报告明确提出不允许明文参数传输,因为数据在传输的过程中可能被拦截,被监听,所以在传输数据的时候使用数据的原始内容进行传输的话,安全隐患是非常大的 ...

  3. 【spring data jpa】spring data jpa 中的update 更新字段,如果原字段值为null不处理,不为null则在原来的值上加一段字符串

    示例代码: /** * 如果barCode字段值为null则不处理 * 如果barCode字段值不为null则在原本值的前面拼接 del: * @param dealer * @return */ @ ...

  4. Kubernetes环境下调整WebLogic JVM基本参数

    基于Kubernetes启动WebLogic后,发现JVM的最大heap size一直在700多M左右,通过 kubectl logs 察看pod启动状态,发现日志中并没有-Xms和-Xmx参数.日志 ...

  5. Linux查找并删除重复文件的命令行fdupes工具,dupeGuru图形工具

    查了几十个网页,找到这个接近满意的解决方案http://unix.stackexchange.com/questions/146197/fdupes-delete-files-aft... 不过正则里 ...

  6. shell脚本之检查局域网中在线的ip地址

    [root@docker-node1 ]# cat ping.sh #!/bin/bash . /etc/init.d/functions for var in {1..254}; do ip=192 ...

  7. javascript快速入门24--XML基础

    XML简介 XML代表Extensible Markup Language(eXtensible Markup Language的缩写,意为可扩展的标记语言).XML 被设计用来传输和存储数据.XML ...

  8. asp.net权限控制的方式

    我们在使用asp.net开发Web程序的时候经常需要进行一些权限控制,如: 限制用户没有登陆就无法查看一些页面,又或者是说登陆之后如果不是管理员,或是没有响应的权限就无法进行相关的操作. 实现的方法有 ...

  9. BZOJ 4174 tty的求助 莫比乌斯反演

    题目大意:求∑Nn=1∑Mm=1∑m−1k=0⌊nk+xm⌋ mod 998244353 如果n和m都已经确定了.如今要求这坨玩应: ∑m−1k=0⌊nk+xm⌋ =∑m−1k=0(⌊nk%m+xm⌋ ...

  10. 深入浅出CChart 每日一课——快乐高四第六课 二丫的青梅,返璞归真之普通窗体多区域画图

    有好些朋友给我反映,就是一个窗体中加入好几个CChartWnd之后.工作不正常.这个的确是这样,CChartWnd会接管原来窗体的消息循环,加入多个CChartWnd之后,就相当于出租房转手好几道,消 ...