封装ShareSDK中的分享功能封以及对类似第三方功能封装的心得【原创】

本篇的主题有三个:

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  */
22 - (void)platShareView:(UIView *)view WithShareContent:(NSString *)shareContent WithShareUrlImg:(NSString *)shareUrlImg WithShareTitle:(NSString *)shareTitle WithShareId:(NSNumber *)shareId WithShareType:(kShareType *)shareType;
23
24 @end

AppDelegate+shareSDK.m

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

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

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

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

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

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

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

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

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

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

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

时间: 2024-08-09 02:17:51

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

Android ShareSDK快速实现分享功能

第一步 :获取ShareSDK 为了集成ShareSDK,您首先需要到ShareSDK官方网站注册并且创建应用,获得ShareSDK的Appkey,然后到SDK的下载页面下载SDK的压缩包,解压以后可以得到如下图的目录结构: ShareSDK在“ShareSDK for Android”目录下,此目录中的“Libs”包含“MainLibs”和“OnekeyShare” 分别是ShareSDK的核心库和“快捷分享”的源码库,说明文档也在“ShareSDK for Android”目录下,集成Sha

React Native(十五)&mdash;&mdash;RN中的分享功能

终于,终于,可以总结自己使用RN时的分享功能了-- 为什么呢?且听我慢慢道来吧: 从刚开始接触React Native(2017年9月中旬)就着手于分享功能,直到自己参与公司的rn项目开发中,再到现在几乎"竣工"的过程中,这一路的"艰辛"估计也只有自己能体会到了吧.其实自己并不喜欢抱怨,也不喜欢把负能量带给身边的朋友,因此在遇到问题后,都是竭尽全力的攻克它,也许会"废寝忘食",也许是"徒劳无功",即使中间道路实在太曲折,但庆幸

Android - 小功能 - 利用最新版ShareSDK进行手动分享(自定义分享界面)

之前有用过Share SDK进行快捷分享,可是官方demo中的快捷分享的界面已经设置死了,而公司的产品又设计了自己的分享界面,这就需要我进行手动分享了. 看了一堆官方的文档,终于写出来了,好了,不废话,进入主题. 之前没有用过ShareSDK分享过的朋友建议先看看官方的文档,不要火急火急的就像照搬官方的demo, 此为文档地址:: http://wiki.sharesdk.cn/Android_快速集成指南 此为官方demo下载地址:http://sharesdk.cn/Download 此为我

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

尊重原创:http://blog.csdn.net/yuanzeyao/article/details/38781957 第三方登陆和分享功能在目前大部分APP中都有,分享功能可以将自己觉得有意义的东西分享给身边的朋友,而第三方登陆可以借助已经有巨大用户基础的平台(如QQ和新浪微博)的账号,让用户在使用自己APP的时候不用注册,直接使用第三方账号登陆,从而避免了注册的过程(其实是服务器端帮你注册),这对于吸引更多的用户非常有意义.下面我们就以新浪微博为例,讲解如何实现分享功能和第三方登陆.首先你

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

因为工作的需求之前也封装过一个JS分享插件,集成了我们公司常用的几个分享平台. 但是总感觉之前的结构上很不理想,样式,行为揉成一起,心里想的做的完美,实际上总是很多的偏差,所以这次我对其进行了改版. 这次的核心就是:JS只负责事件+结构,也就是把功能实现出来,具体的外观样式,则使用者自己进行定义. 以下是新版分享插件的代码: 1 (function(root){ 2 'use strict'; 3 function share(params){ 4 5 this.params = params;

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

首先说一下 ionic 是单页面应用,也就是说整个项目就有一个index.html, 那么问题就就来了, 如果我们不同的页面要分享给大家的是不同的链接和图片,应该怎么去做呢? 这就是我们今天要总结的东西. 今天这个问题真是闹得我心烦,有必要总结下来了. 学习重点: 微信分享方法巧妙封装 监听路由事件$rootScope.$on 举一反三 微信分享 关于微信分享,大家都是在熟悉不过了,无非就是调用微信的SDK,授权,给他分享索要的东西的ok.所以对于微信分享似乎没有什么好说的,但是细心的伙伴有木有

shareSDK的初步使用(shareSDK中微信、qq等兼容问题,以及cocoapods支持架构冲突问题的解决)

第一次使用shareSDK来做第三方分享,可是.昨天一天都是在调试bug,一直错误不断! 先说下我的开发环境: xcode:5.1 真机调试:iPhone5s 我们都知道xcode5.1以后開始是支持arm64的,而shareSDK中的微信.qq是不支持armv64的,所以网上都有非常多方法都是正确的改变xcode的编译条件,改成armv7,armv7s:详细怎么改我就不多说了,报错直接Google一下就知道了.关键是这个问题攻克了,问题又来了我有的开源库管理工具cocoapods来实现的安装第

友推快速实现分享到微信等各大平台功能

一直想做一个基于友推的系列专题(包括appKey申请和开发过程中常遇到的一些问题),帮助大家更好地去实现分享功能,总结一个下午,总算弄出一些皮毛来,以后会持续更新,包括一些安卓开发中常遇到的一些问题以及经验奉献给大家分享到微信 分享到微信分享到微信分享到微信分享到微信分享到微信分享到微信分享到微信分享到微信分享到微信分享到微信分享到微信分享到微信分享到微信分享到微信分享到微信一:已实现功能 1.支持微信,QQ,新浪微博,QQ空间,短信,邮件等多家大型社交媒体平台一键分享2.支持积分抽奖活动在线活

ios学习:swift中实现分享到微博、facebook,twitter等

在swift中打开分享功能原来是如此的简单. 1.首先须要 import Social 2.在分享button事件以下 var controller:SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeSinaWeibo) controller.setInitialText("一起来swift吧!") controller.addImage(UIImage(named: "