本篇的主題有三個:
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類型,如果不行在選自動。