极光征文程序员码农的世界

极光征文|如何通过cocoapods轻松管理项目中的极光推送业务

2019-01-21  本文已影响38人  Nicar

极光征文|通过cocoapods管理项目中的极光推送业务

第三方业务繁多带来的问题

        随着公司业务的不断扩展,开发工作中要面临的业务耦合问题越来越严重,一处修改全局生效的方案也越来越不能满足代码更新的需要,为了避免过多的二次coding,就要考虑去耦合。将各个小的业务逐渐从主业务代码中剥离出来成为一个独立的单元来管理,变得越来越迫切。在iOS开发行业中,很多优秀的组件化方案层出不穷,在这个过程中,不可避免的会接入第三方业务,由于第三方业务的接入,可能会导致某些功能在项目中到处引用,一旦发生变动,就需要全局找代码修改,因此,第三方库的组件化管理也将变得越来越重要。

对于极光推送业务API管理的一些想法

        前几天更新了一个老项目里面的SDK,其中就包含极光推送的SDK,由于这块业务很少会发生变动,期间一直没有留意过,更新之后就发现,很多古老的API都废弃了,崩溃,项目中相关的API使用也有蛮多的,查看了相关的代买自后,发现,变化不是特别大,那么我可以将相关的API根据自身的业务需要进行二次封装(这里就不提了,本篇文章着重组件化)。随后又想了一下,如果以后再有类似的更新怎么办呢?我总不能每次都在主工程找代码改吧?那么组件化管理的方案首先就进入了我的脑海,毕竟前段时间将其他的SDK一股脑儿的全都从主工程干掉了,但是还没有将极光推送业务放进来,那正好收拾一下这一块。

使用cocoapods将极光推送业务进行组件化

    首先把业务代码给写好,那么不废话,先上代码

    JPushApiManagerDelegate 继承`JPUSHRegisterDelegate`协议,以方便扩展协议

@protocol JPushApiManagerDelegate <JPUSHRegisterDelegate>

@end

* JPushApiManager 单利设计,同事提供注册业务API,注册极光服务只需一个API即可

@property (nonatomic, weak ) id<JPushApiManagerDelegate> delegate;

+ (instancetype)sharedManager;

/**

注册极光服务

@param appKey appKey

@param launchOptions launchOptions

@param delegate delegate

*/

- (void)setJPushAppKey:(nonnull NSString *)appKey didFinishLaunchingWithOptions:(nonnull NSDictionary *)launchOptions delegate:(nonnull id<JPushApiManagerDelegate>)delegate;

@end

* APPDelegate

一行代码即可搞定,didfinishlaunching方法中的代码尽可能保持干净

#import "AppDelegate.h"

#import "JPushApiManager.h"

static NSString * const JPushAppKey = @"在这里填写你的极光推送AppKey";

@interface AppDelegate ()<JPushApiManagerDelegate>

@end

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    [[JPushApiManager sharedManager] setJPushAppKey:JPushAppKey didFinishLaunchingWithOptions:launchOptions delegate:self];

    return YES;

}

JPUSHRegisterDelegate 写到APPdelegate里

// iOS 10 Support

- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler {

    // Required

    NSDictionary * userInfo = notification.request.content.userInfo;

    if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {

        [JPUSHService handleRemoteNotification:userInfo];

    }

    completionHandler(UNNotificationPresentationOptionAlert); // 需要执行这个方法,选择是否提醒用户,有Badge、Sound、Alert三种类型可以选择设置

}

// iOS 10 Support

- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {

    // Required

    NSDictionary * userInfo = response.notification.request.content.userInfo;

    if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {

        [JPUSHService handleRemoteNotification:userInfo];

    }

    completionHandler();

}

// iOS 12

- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center openSettingsForNotification:(nullable UNNotification *)notification {

}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {

    // Required, iOS 7 Support

    [JPUSHService handleRemoteNotification:userInfo];

    completionHandler(UIBackgroundFetchResultNewData);

}

* 至此,相关的编码工作就完成了,接下来就是使用cocoapods将代码进行组件化管理

在工程根目录下运行如下代码

$ pod spec create JPushApiManager

$ vim JPushApiManager.podspec

修改.podspec文件内容如下

Pod::Spec.new do |s|

  s.name        = "JPushApiManager"

  s.version      = "3.1.2.1"

  s.summary      = "组件化方式管理极光推送代码"

  s.description  = <<-DESC

                    组件化方式管理极光推送代码,以方便代码中一处修改全局生效

                  DESC

  s.homepage    = "https://github.com/XFNicar/JPushApiManager"

  s.license      = { :type => "MIT", :file => "LICENSE" }

  s.author            = { "XieFei" => "xuejingwen25@163.com" }

  s.platform    = :ios, "9.0"

  s.source      = { :git => "https://github.com/XFNicar/JPushApiManager.git", :tag => "#{s.version}" }

  s.source_files  = "JPushApiManager/JPushApiManager/*.{h,m}"

  s.requires_arc = true

  # s.ios.vendored_libraries = 'libs/*.a'

  s.dependency "JPush", "~> 3.1.2"

end

提交代码

$ git add .

$ git commit -m "nothing"

$ git push origin master

$ git tag -a 3.1.2.1 -m "发布3.1.2.1版本"

$ git push origin --tags

校验.podspec文件的正确性

$ pod lib lint --use-libraries --allow-warnings

发布代码

$ pod trunk push JPushApiManager.podspec

看到如下效果就说明发布成功了

--------------------------------------------------------------------------------

🎉  Congrats

🚀  JPushApiManager (3.1.2.1) successfully published

📅  January 20th, 23:10

🌎  https://cocoapods.org/pods/JPushApiManager

👍  Tell your friends!

--------------------------------------------------------------------------------

在工程中导入代码

pod 'JPushApiManager', '~> 3.1.2.1'

说在后面

至此极光推送业务组件化管理的相关工作就结束了,组件化带来的好处是很明显的,此次组件化的目的有三点:

1. 如果项目中有部分需要主动触发推送业务的代码,那么就可以统一定制API,防止官方API发生改变之后,需要重新搜索代码来修改。

2. 将业务进行剥离,可以单独在一个模块里管理极光推送业务。

3. 让项目中代码更加干净整洁。

4. 装逼。。。2333333

「本文为极光征文参赛文章」

上一篇 下一篇

猜你喜欢

热点阅读