JRHModuleManager
简介
AppDelegate 精简方案 Swift 版本,需要预先注册需要精简的模块,然后在 AppDelegate
中的代理方法中调用一个中间件 JRHModuleManager
,JRHModuleManager
负责集中分发给已注册并实现协议的模块。
CocoaPods 导入
pod 'JRHModuleManager', '~> 1.0.4'
使用
在 AppDelegate.swift
文件中的 didFinishLaunchingWithOptions
方法中注册模块,只需注册一次
提示
可以创建 plist 文件来管理需要注册的模块,以便于维护
调用
// 1. 先提前注册模块,只需要注册一次
let plistPath = Bundle.main.path(forResource: "JRHModuleManager", ofType: "plist")!
JRHModuleManager.shareInstance().loadModulesWithPlistFile(filePath: plistPath)
// 2. 在App启动的时候调用模块
_ = JRHModuleManager.shareInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
同样,可以为 AppDelegate 的任何方法进行精简
// 进入后台/退出程序
func applicationDidEnterBackground(_ application: UIApplication) {
_ = JRHModuleManager.shareInstance().applicationDidEnterBackground(application)
}
// 从后台回来点击进入应用
func applicationWillEnterForeground(_ application: UIApplication) {
_ = JRHModuleManager.shareInstance().applicationWillEnterForeground(application)
}
// 关闭程序
func applicationWillTerminate(_ application: UIApplication) {
_ = JRHModuleManager.shareInstance().applicationWillTerminate(application)
}
最后在具体的类中操作,也可以在之前已经注册好的模块类中操作,假设我们有一个已经注册的AppTimeLineModule.swift
模块,那么就在此类中实现相应需要处理的方法
// App启动
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
print("didFinishLaunchingWithOptions")
// code ...
return true
}
// 进入后台/退出程序
func applicationDidEnterBackground(_ application: UIApplication) {
print("applicationDidEnterBackground")
// code ...
}
// 从后台回来点击进入应用
func applicationWillEnterForeground(_ application: UIApplication) {
print("applicationWillEnterForeground")
// code ...
}
// 关闭程序
func applicationWillTerminate(_ application: UIApplication) {
print("applicationWillTerminate")
// code ...
}