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

来源:互联网 发布:java官方下载地址 编辑:程序博客网 时间:2024/06/08 04:42

本篇的主题有三个:

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 *)shareType132 {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:imageArray151 //                                        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:nil185 //                                                                   delegate:nil186 //                                                          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:nil196 //                                                          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 251252 @end
复制代码

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

 

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

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

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

 

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

 

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

 

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

 

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

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

1 0