封装ShareSDK中的分享功能封以及对类似第三方功能封装的心得【原创】
本篇的主题有三个:
1、封装思想的介绍
2、我的封装代码
3、我在封装sharesdk(采用的是简洁版本)分享功能是碰到的问题,以及解决方法。
PS:其实这个我之前封装过一次,不过最近在重构项目时发现,当时封装的是如此的垃圾,所以在这里再来一次。欢迎大家批评纠错。
封装思想
因为这次封装的第三方SDK的功能,所以我采用延展的方式来进行封装。这样有以下两种好处:
1、 这样将第三方功能给模块化,在项目中方便查找和修改。
2、 很多第三方功能都是需要在appdelegae初始化,采用category只需在扩展的类中申明一个public方法,将初始化的代码放在相应的分类public中即可。最 后只需在appdelegate调用相应的功能模块初始化方法即可。
下面两张图,是我的延展类的形式和我在项目中封装两个第三方功能后,Appdelegate中的代码情况。
ShareSDK功能的封装
AppDelegate+ShareSDk.h
- //
- // AppDelegate+ShareSDk.h
- // CDL_optimize
- //
- // Created by 王立广 on 15/9/11.
- // Copyright (c) 2015年 王立广. All rights reserved.
- //
- #import "AppDelegate.h"
- @interface AppDelegate (ShareSDk)
- /**
- * shareSDK分享
- */
- - (void)addShareSDKWithapplication:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;
- /**
- * 定制平台分享内容分享
- */
- - (void)platShareView:(UIView *)view WithShareContent:(NSString *)shareContent WithShareUrlImg:(NSString *)shareUrlImg WithShareTitle:(NSString *)shareTitle WithShareId:(NSNumber *)shareId WithShareType:(kShareType *)shareType;
- @end
AppDelegate+shareSDK.m
- // AppDelegate+ShareSDk.m
- // CDL_optimize
- //
- // Created by 王立广 on 15/9/11.
- // Copyright (c) 2015年 王立广. All rights reserved.
- //
- #import "AppDelegate+ShareSDk.h"
- #import <ShareSDK/ShareSDK.h>
- #import <ShareSDKExtension/SSEShareHelper.h>
- #import <ShareSDKUI/ShareSDK+SSUI.h>
- #import <ShareSDKUI/SSUIShareActionSheetStyle.h>
- #import <ShareSDKUI/SSUIShareActionSheetCustomItem.h>
- #import <ShareSDK/ShareSDK+Base.h>
- #import <ShareSDK/ShareSDK.h>
- #import <TencentOpenAPI/QQApiInterface.h>
- #import <TencentOpenAPI/TencentOAuth.h>
- #import "WXApi.h"
- #import "WeiboSDK.h"
- #import <ShareSDKConnector/ShareSDKConnector.h>
- //新浪微博
- #define kSinaWeiboAPPKey @"*********"
- #define kSinaWeiboAPPSecret @"************"
- //腾讯微博
- #define kTencentWeiboAPPKey @"*********"
- #define kTencentWeiboAPPSecret @"**********"
- #define kQQAPPId @"**********"
- #define kQQAPPKey @"**********"
- //微信
- #define kWechatAPPId @"*************"
- #define kWechatAPPSecret @"************"
- //下面这个枚举用来判断分享哪个模块,建议放在pch文件中
- //typedef enum
- //{
- // shareDartbar,//镖吧分享
- // shareInfo, //资讯分享
- //
- //}kShareType;
- @implementation AppDelegate (ShareSDk)
- - (void)addShareSDKWithapplication:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
- {
- //初始化配置
- [self shareInit];
- }
- #pragma mark 分享平台初始化
- - (void)shareInit
- {
- NSArray *platformArray = [NSArray array];
- platformArray = @[@(SSDKPlatformTypeSinaWeibo),
- @(SSDKPlatformTypeTencentWeibo),
- @(SSDKPlatformTypeWechat),
- @(SSDKPlatformTypeQQ),
- ];
- /**
- * 构造分享平台
- *
- * @param platformType 分享平台
- *
- * @param onImport 此时如果要分享到一些客户端这个block块必须要填。
- *
- * @param onConfiguration appkey的相关配置
- */
- [ShareSDK registerApp:@"712aaee4e6ee" activePlatforms:platformArray
- onImport:^(SSDKPlatformType platformType) {
- switch (platformType)
- {
- case SSDKPlatformTypeWechat:
- [ShareSDKConnector connectWeChat:[WXApi class]];
- break;
- case SSDKPlatformTypeQQ:
- [ShareSDKConnector connectQQ:[QQApiInterface class] tencentOAuthClass:[TencentOAuth class]];
- break;
- default:
- break;
- }
- }
- onConfiguration:^(SSDKPlatformType platformType, NSMutableDictionary *appInfo) {
- switch(platformType)
- {
- case SSDKPlatformTypeSinaWeibo:
- //设置新浪微博应用信息,其中authType设置为使用SSO+web形式授权
- [appInfo SSDKSetupSinaWeiboByAppKey:kSinaWeiboAPPKey appSecret:kSinaWeiboAPPSecret redirectUri:@"http://www.sharesdk.cn" authType:SSDKAuthTypeBoth];
- break;
- case SSDKPlatformTypeTencentWeibo:
- //设置腾讯微博应用信息,其中authType只能使用web形式授权
- [appInfo SSDKSetupTencentWeiboByAppKey:kTencentWeiboAPPKey appSecret:kTencentWeiboAPPSecret redirectUri:@"http://www.sharesdk.cn"];
- break;
- case SSDKPlatformTypeQQ:
- //QQ平台
- [appInfo SSDKSetupQQByAppId:kQQAPPId appKey:kQQAPPKey authType:SSDKAuthTypeBoth];
- break;
- case SSDKPlatformTypeWechat:
- //微信平台
- [appInfo SSDKSetupWeChatByAppId:kWechatAPPId appSecret:kWechatAPPSecret];
- break;
- }
- }];
- }
- - (void)platShareView:(UIView *)view WithShareContent:(NSString *)shareContent WithShareUrlImg:(NSString *)shareUrlImg WithShareTitle:(NSString *)shareTitle WithShareId:(NSNumber *)shareId WithShareType:(kShareType *)shareType
- {
- NSString *shareUrl = nil;
- if(shareType == shareInfo){
- shareUrl = kInfoShareRequest(shareId);
- }else{
- shareUrl = kDartBarShareRequest(shareId);
- }
- //创建分享参数
- NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
- #pragma mark 公共分享参数
- // [shareParams SSDKSetupShareParamsByText:@"分享内容"
- // images:imageArray
- // url:[NSURL URLWithString:@"http://mob.com"]
- // title:@"分享标题"
- // type:SSDKContentTypeImage];
- #pragma mark 平台定制分享参数
- //新浪微博
- [shareParams SSDKSetupSinaWeiboShareParamsByText:[NSString stringWithFormat:@"%@ %@",shareContent,shareUrl] title:shareTitle image:kLoadNetImage(shareUrlImg) url:nil latitude: longitude: objectID:nil type:SSDKContentTypeAuto];
- //腾讯微博
- [shareParams SSDKSetupTencentWeiboShareParamsByText:[NSString stringWithFormat:@"%@ %@",shareContent,shareUrl] images:kLoadNetImage(shareUrlImg) latitude: longitude: type:SSDKContentTypeText];
- //QQ空间
- [shareParams SSDKSetupQQParamsByText:nil title:shareTitle url:[NSURL URLWithString:shareUrl] thumbImage:kLoadNetImage(shareUrlImg) image:kLoadNetImage(shareUrlImg) type:SSDKContentTypeWebPage forPlatformSubType:SSDKPlatformSubTypeQZone];
- //QQ好友
- [shareParams SSDKSetupQQParamsByText:nil title:shareTitle url:[NSURL URLWithString:shareUrl] thumbImage:kLoadNetImage(shareUrlImg) image:kLoadNetImage(shareUrlImg) type:SSDKContentTypeWebPage forPlatformSubType:SSDKPlatformSubTypeQQFriend];
- //微信收藏
- [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];
- //微信好友
- [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];
- //微信朋友圈
- [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];
- #pragma mark 不跳过编辑界面的分享框
- // [ShareSDK showShareActionSheet:view items:[ShareSDK activePlatforms] 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 *alertView = [[UIAlertView alloc] initWithTitle:@"分享失败"
- // message:[NSString stringWithFormat:@"%@", error]
- // delegate:nil
- // cancelButtonTitle:@"确定"
- // otherButtonTitles:nil];
- // [alertView show];
- // break;
- // }
- // case SSDKResponseStateCancel:
- // {
- // break;
- // }
- // default:
- // break;
- // }
- // }];
- #pragma mark 设置跳过分享编辑页面,直接分享的平台。
- SSUIShareActionSheetController *sheet = [ShareSDK showShareActionSheet:view 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 *alertView = [[UIAlertView alloc]initWithTitle:@"分享失败"
- message:[NSString stringWithFormat:@"%@",error] delegate:nil cancelButtonTitle:@"确定"otherButtonTitles:nil];
- [alertView show];
- break;
- }
- case SSDKResponseStateCancel:
- {
- break;
- }
- default:
- break;
- }
- }];
- //删除和添加平台示例
- [sheet.directSharePlatforms addObject:@(SSDKPlatformTypeSinaWeibo)];
- [sheet.directSharePlatforms addObject:@(SSDKPlatformTypeTencentWeibo)];
- }
- @end
PS:在代码里注释我都加上去了,个人感觉算是十分详细了,如果有问题,可以留下你的留言。
封装过程中碰到的问题以及解决方法
1、面板上一直显示不出来,相应的分享平台
在shareSDK的初始化方法中,有个onImport参数,如果分享的到app里,要传递这个参 数,要不然,在面板中不会显示这些平台的
2、新浪微博分享时,怎么才能将shareSdk给的界面里填的分享内容(这个界面默认是),分享到新浪微博里
只有分享的参数是公共的时候,在编辑页面修改的内容才会显示在分享的平台上。如果是给各个平台定制分享内容的话,在编辑页面修改的内容不会显示在分享的平台上,另外此时需要隐藏编辑界面,在代码中已注释。
3、在平台分享时我选择的是自动匹配分享类型,但我分享的内容没有图片时却分享不成功
选在分享类型的时候,能确定属于哪个类型,就选择哪个,如果实在确定不了就选自动
如果分享的内容有url的时候,一般选择SSDKContentTypeWebPage类型,如果不行在选自动。
4、分享到腾讯微博、新浪微博,要添加连接时,在内容后面填上链接。
封装ShareSDK中的分享功能封以及对类似第三方功能封装的心得【原创】的更多相关文章
- ShareSDK中微信分享错误总结
项目中用到微信分享,可向好友或朋友圈分享链接时,分享人可以打开网站,查看消息者却始终不能打开网站.试了N种方法,重写了N次分享模块,均没办法解决. 在无意中查看分享链接时发现,朋友圈里分享后,原始链接 ...
- 在Android Studio中使用shareSDK进行社会化分享(图文教程)
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...
- React Native(十五)——RN中的分享功能
终于,终于,可以总结自己使用RN时的分享功能了-- 为什么呢?且听我慢慢道来吧: 从刚开始接触React Native(2017年9月中旬)就着手于分享功能,直到自己参与公司的rn项目开发中,再到现在 ...
- 自己封装的一个JS分享组件
因为工作的需求之前也封装过一个JS分享插件,集成了我们公司常用的几个分享平台. 但是总感觉之前的结构上很不理想,样式,行为揉成一起,心里想的做的完美,实际上总是很多的偏差,所以这次我对其进行了改版. ...
- TOP100summit:【分享实录-封宇】58到家多端消息整合之路
本篇文章内容来自2016年TOP100summit 58到家架构师封宇的案例分享. 编辑:Cynthia 2017年11月9-12日北京国家会议中心第六届TOP100summit,留言评论有机会获得免 ...
- Android 中实现分享和第三方登陆---以新浪微博为例
第三方登陆和分享功能在目前大部分APP中都有,分享功能可以将自己觉得有意义的东西分享给身边的朋友,而第三方登陆可以借助已经有巨大用户基础的平台(如QQ和新浪微博)的账号,让用户在使用自己APP的时候不 ...
- 分享公司DAO层动态SQL的一些封装
主题 公司在DAO层使用的框架是Spring Data JPA,这个框架很好用,基本不需要自己写SQL或者HQL就能完成大部分事情,但是偶尔有一些复杂的查询还是需要自己手写原生的Native SQL或 ...
- 手机QQ内置网页,微信内置网页中进行分享到QQ和微信的操作
微信内的网页分享: API内容详见微信开发文档 https://mp.weixin.qq.com/wiki 这里需要注意的是:调用微信API的时候修改的是微信内网页右上角三个点那里打开后,选择分享之 ...
- ionic单页面应用中微信分享的问题总结
首先说一下 ionic 是单页面应用,也就是说整个项目就有一个index.html, 那么问题就就来了, 如果我们不同的页面要分享给大家的是不同的链接和图片,应该怎么去做呢? 这就是我们今天要总结的东 ...
随机推荐
- 使用Maven下载jar包
有些开源项目不直接提供jar包的下载,而是建议使用Maven下载,以开源库hipster(https://github.com/citiususc/hipster,http://www.hipster ...
- java前后端加密(转载)
最近做一个项目的安全渗透测评,测评人员发来一份测试报告,报告明确提出不允许明文参数传输,因为数据在传输的过程中可能被拦截,被监听,所以在传输数据的时候使用数据的原始内容进行传输的话,安全隐患是非常大的 ...
- 【spring data jpa】spring data jpa 中的update 更新字段,如果原字段值为null不处理,不为null则在原来的值上加一段字符串
示例代码: /** * 如果barCode字段值为null则不处理 * 如果barCode字段值不为null则在原本值的前面拼接 del: * @param dealer * @return */ @ ...
- Kubernetes环境下调整WebLogic JVM基本参数
基于Kubernetes启动WebLogic后,发现JVM的最大heap size一直在700多M左右,通过 kubectl logs 察看pod启动状态,发现日志中并没有-Xms和-Xmx参数.日志 ...
- Linux查找并删除重复文件的命令行fdupes工具,dupeGuru图形工具
查了几十个网页,找到这个接近满意的解决方案http://unix.stackexchange.com/questions/146197/fdupes-delete-files-aft... 不过正则里 ...
- shell脚本之检查局域网中在线的ip地址
[root@docker-node1 ]# cat ping.sh #!/bin/bash . /etc/init.d/functions for var in {1..254}; do ip=192 ...
- javascript快速入门24--XML基础
XML简介 XML代表Extensible Markup Language(eXtensible Markup Language的缩写,意为可扩展的标记语言).XML 被设计用来传输和存储数据.XML ...
- asp.net权限控制的方式
我们在使用asp.net开发Web程序的时候经常需要进行一些权限控制,如: 限制用户没有登陆就无法查看一些页面,又或者是说登陆之后如果不是管理员,或是没有响应的权限就无法进行相关的操作. 实现的方法有 ...
- 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⌋ ...
- 深入浅出CChart 每日一课——快乐高四第六课 二丫的青梅,返璞归真之普通窗体多区域画图
有好些朋友给我反映,就是一个窗体中加入好几个CChartWnd之后.工作不正常.这个的确是这样,CChartWnd会接管原来窗体的消息循环,加入多个CChartWnd之后,就相当于出租房转手好几道,消 ...