一般集成使用一些第三方SDK都需要在AppDelegate中注冊(cè)初始化,初始化的方式無(wú)關(guān)幾種:
- 1.極品的程序猿會(huì)在didFinishLaunchingWithOptions一個(gè)方法中從頭搞到尾,后期維護(hù)到吐
- 2.在AppDelegate.m中為每一種SDK都抽取出來(lái)一個(gè)方法,后期修改稍微好點(diǎn),但是一旦第三方SDK多點(diǎn),那么.m里面的代碼會(huì)變得巨多,動(dòng)則上千行,另外其它項(xiàng)目使用到同樣的SDK,只能粘貼、復(fù)制、粘貼、復(fù)制……
- 3.最好的辦法:創(chuàng)建SDK對(duì)應(yīng)的工具類或者分類,把注冊(cè)初始化的代碼完全抽取出來(lái),最好在AppDelegate中一句代碼搞定,這感覺爽到爆
下面是封裝的ShareSDK、微信支付、支付寶支付、極光推送對(duì)應(yīng)的幾個(gè)分類,把分類拖進(jìn)項(xiàng)目,幾句代碼搞定全部。
創(chuàng)建的幾個(gè)分類
#import "AppDelegate+ShareSDK.h"
#import "AppDelegate+WXApi.h"
#import "AppDelegate+AlipaySDK.h"
#import "AppDelegate+JPushSDK.h"
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//初始化所有的第三方SDK
[self setUpThridPartySDKWithOptions:launchOptions];
return YES;
}
- (void)setUpThridPartySDKWithOptions:(NSDictionary *)launchOptions
{
//注冊(cè)ShareSDK
[AppDelegate registerShareSDK];
//注冊(cè)極光推送
[AppDelegate registerJPushSDKWithOptions:launchOptions];
//注冊(cè)微信支付
[AppDelegate registerWeChatWithAppID:@"AppID"];
[AppDelegate registerWXPayWithMchID:@"MchID" appSecret:@"Secret"];//客戶端簽名時(shí)調(diào)用注冊(cè)
//注冊(cè)支付寶支付
[AppDelegate registerAlipayWithPartnerID:@"PartnerID" sellerID:@"sellerID" partnerPrivKey:@"PrivKey"];//客戶端簽名時(shí)調(diào)用注冊(cè)
}
分享內(nèi)容API
/**
* 定制平臺(tái)分享內(nèi)容分享
*/
+ (void)platShareView:(UIView *)view WithShareContent:(NSString *)shareContent WithShareUrlImg:(NSString *)shareUrlImg WithShareTitle:(NSString *)shareTitle WithHTMLURL:(NSString *)URL;
支付寶支付API
/**
* 發(fā)起支付(客戶端簽名版本)
*
* @param orderID 訂單號(hào)
* @param orderName 訂單標(biāo)題
* @param orderDescription 訂單描述
* @param orderPrice 訂單價(jià)格,保留小數(shù)點(diǎn)2位,單位(元)
* @param orderNotifyUrl 服務(wù)端回調(diào)URL(重要)
* @param appScheme 設(shè)置的app的URLScheme
* @param config 支付完成后的回調(diào)(無(wú)論是網(wǎng)頁(yè)版本還是支付寶客戶端的版本都通過(guò)此block回調(diào))(successed = YES 代表支付成功)
*/
+ (void)sendAlipayPayRequestWithOrderID:(NSString *)orderID
orderName:(NSString *)orderName
orderDescription:(nullable NSString *)orderDescription
orderPrice:(NSString *)orderPrice
orderNotifyUrl:(NSString *)orderNotifyUrl
appScheme:(NSString *)appScheme
callbackConfig:(void (^)(BOOL successed))config;
微信支付API
/**
* 發(fā)起支付 (客戶端簽名版本)
*
* @param orderID 訂單ID
* @param orderName 訂單標(biāo)題
* @param orderPrice 訂單價(jià)格,單位分,不能有小數(shù)點(diǎn)
* @param orderNotifyUrl 服務(wù)器回調(diào)URL(重要)
* @param config 支付完成后的回調(diào)(successed = YES 代表支付成功)
*/
+ (void)sendWeChatPayRequestWithOrderID:(NSString *)orderID
orderName:(NSString *)orderName
orderPrice:(NSString *)orderPrice
orderNotifyUrl:(NSString *)orderNotifyUrl
callbackConfig:(void (^)(BOOL successed))config;
其它的一些第三方SDK也可以按照這樣封裝,具體實(shí)現(xiàn)代碼下載地址地址鏈接,注意:因?yàn)榈谌絊DK.a太大不好上傳,所以沒有添加。