封装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 @"**********" //QQ
#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, 那么问题就就来了, 如果我们不同的页面要分享给大家的是不同的链接和图片,应该怎么去做呢? 这就是我们今天要总结的东 ...
随机推荐
- Delphi 二维码产生和扫描
Zint用于产生二维码. Zxing用读取二维码. VFrames.pas和VSample.pas用于摄像头. 另附带摄像头相关的类库,也可用开源的dspack也可用于摄像头的需求. 以上为开源的信息 ...
- SqlMapConfig.xml详细介绍
1,连接数据库 <!--配置环境,默认的环境id为oracle --> <environments default="oracle"> <!-- 配置 ...
- CSS box-flex属性,然后弹性盒子模型简介(转)
一.淡淡的开头语 昨天趁着不想工作的时间间隙闲逛24ways,在My CSS Wish List一文中,见到了个新鲜的CSS属性,就是题目中的box-flex,以前没有见过,顿生疑惑,不知是骡子还是马 ...
- SweetAlert和MBProgressHUD冲突的解决办法
解决方法 : UIView *view = [UIApplication sharedApplication].keyWindow; NSLog(@" -- &g ...
- Could not instantiate bean class [org.springframework.data.domain.Pageable]: Specified class is an interface解决方案
原文:http://pimin.net/archives/432 环境:Eclipse LUNA.Spring 4.1.1.或Spring 4.3.3.Spring Data Elasticsearc ...
- Mysql的共享锁和排他锁(转载)
mysql锁机制分为表级锁和行级锁,本文就和大家分享一下我对mysql中行级锁中的共享锁与排他锁进行分享交流. 共享锁又称为读锁,简称S锁,顾名思义,共享锁就是多个事务对于同一数据可以共享一把锁,都能 ...
- [Linux] Ubuntu下非常给力的下载工具
转载:http://blog.csdn.net/luojiming1990/article/details/9078447 Windows下的下载工具--迅雷,之所以下载速度快,乃是它能搜索资源.为己 ...
- 利用CMD查看系统硬件信息
利用CMD查看系统硬件信息对于在windows下查看系统信息大家一定不陌生了,我现在说几个最常用的方法,对命令感兴趣的朋友看看,(给菜鸟看的,老手就不要笑话我了,大家都是从那个时候过来的,^_^).一 ...
- Install FileZilla in Ubuntu16.04
一.安装fileZilla $ sudo add-apt-repository ppa:n-muench/programs-ppa $ sudo apt-get update $ sudo apt-g ...
- Codeforces 553B Kyoya and Permutation
problem 题意 本题题意不太easy看懂. 给定一个序列,我们能够把这个序列变成一些循环置换的和.然而这样的置换的方法是不止一种的.我们定义一种standard cyclic represent ...