本篇的主题有三个:

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、分享到腾讯微博、新浪微博,要添加连接时,在内容后面填上链接。

最新文章

  1. MongoDB集群配置
  2. 设置UISegmentedControl的字体大小和颜色
  3. PHP基础示例:用PHP+Mysql编写简易新闻管理系统[转]
  4. MFC学习笔记(一)向模态对话框传递数据
  5. HDU 4471 矩阵快速幂 Homework
  6. mysql中datetime比较大小问题 (转的)
  7. 百度 UEditor 简单安装调试和调用,网上其它的教程太官方了,不适合新手
  8. Ffmpeg解析media容器过程/ ffmpeg 源代码简单分析 : av_read_frame()
  9. 浅析JavaScript函数的参数
  10. 利用钩子函数来捕捉键盘响应的windows应用程序
  11. Robot Framework和Selenium 2 Grid集成指南
  12. PHP面向对象摘要
  13. php 写程序求三个数的最大值
  14. Linux-Centos破解安装confluene6.3.1
  15. influence maximization 第二弹
  16. http协议与https协议
  17. SpringBoot----跨域配置
  18. python中 =、copy、deepcopy的差别
  19. Django_博客项目 注册用户引发 ValueError: The given username must be set
  20. C#中的垃圾回收机制与delegate

热门文章

  1. 解决ubuntu系统中wireshark:Couldn&#39;t run /usr/bin/dumpcap in child process: Permission denied的问题
  2. jvm-监控指令-jstack
  3. 请用心练完这16个webpack小例子
  4. Firefox中好用的几个快捷键
  5. mormot支持websocket
  6. 【SQL Server】sql server更改了数据表的字段/新增数据表的字段 无法保存
  7. 规约模式Specification Pattern
  8. coco2dx-2.2.2 win32启动过程(opengl 和 窗口大小初始化部分) - 学习笔记 1
  9. VUE -- Vue.js每天必学之计算属性computed与$watch
  10. Myeclipse中文件已经上传到server文件夹下,文件也没有被占用,可是页面中无法读取和使用问题的解决方法