封裝ShareSDK中的分享功能封以及對相似第三方功能封裝的心得【原創】

本篇的主題有三個:git

一、封裝思想的介紹web

二、個人封裝代碼微信

三、我在封裝sharesdk(採用的是簡潔版本)分享功能是碰到的問題,以及解決方法。app

PS:其實這個我以前封裝過一次,不過最近在重構項目時發現,當時封裝的是如此的垃圾,因此在這裏再來一次。歡迎你們批評糾錯。模塊化

封裝思想url

由於此次封裝的第三方SDK的功能,因此我採用延展的方式來進行封裝。這樣有如下兩種好處:spa

 一、 這樣將第三方功能給模塊化,在項目中方便查找和修改。code

 二、 不少第三方功能都是須要在appdelegae初始化,採用category只需在擴展的類中申明一個public方法,將初始化的代碼放在相應的分類public中便可。最         後只需在appdelegate調用相應的功能模塊初始化方法便可。orm

 

下面兩張圖,是個人延展類的形式和我在項目中封裝兩個第三方功能後,Appdelegate中的代碼狀況。blog

   

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:在代碼裏註釋我都加上去了,我的感受算是十分詳細了,若是有問題,能夠留下你的留言。

 

封裝過程當中碰到的問題以及解決方法

一、面板上一直顯示不出來,相應的分享平臺

    在shareSDK的初始化方法中,有個onImport參數,若是分享的到app裏,要傳遞這個參     數,要否則,在面板中不會顯示這些平臺的

 

二、新浪微博分享時,怎麼才能將shareSdk給的界面裏填的分享內容(這個界面默認是),分享到新浪微博裏

 

只有分享的參數是公共的時候,在編輯頁面修改的內容纔會顯示在分享的平臺上。若是是給各個平臺定製分享內容的話,在編輯頁面修改的內容不會顯示在分享的平臺上,另外此時須要隱藏編輯界面,在代碼中已註釋。

 

三、在平臺分享時我選擇的是自動匹配分享類型,但我分享的內容沒有圖片時卻分享不成功

 

選在分享類型的時候,能肯定屬於哪一個類型,就選擇哪一個,若是實在肯定不了就選自動

若是分享的內容有url的時候,通常選擇SSDKContentTypeWebPage類型,若是不行在選自動。

 

四、分享到騰訊微博、新浪微博,要添加鏈接時,在內容後面填上連接。

相關文章
相關標籤/搜索