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