极光征文 | 通过 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
「本文为极光征文参赛文章」
来源: http://www.jianshu.com/p/58a581199f52