talk is cheap, show me the code
项目背景
聚合SDK接入了数据统计,登录,分享等10多个第三方SDK。
问题
- 第三方SDK的注册配置都在AppDelegate.m里
- 与启动相关的业务代码都在AppDelegate.m里
导致AppDelegate.m臃肿,可读性差,高藕合。
目标
增加删除或修改模块不需要修改AppDelegate.m,保持AppDelegate.m中代码干净整洁低藕合
实现
XPModuleDelegate
XPModuleDelegate继承自UIApplicationDelegate。原本依赖AppDelegate.m的模块都要遵循XPModuleDelegate。
import Foundation
import UIKit
public protocol XPModuleDelegate: UIApplicationDelegate, UNUserNotificationCenterDelegate {
}
ModuleA
各个模块本来在AppDelegate.m中的代码放在自己的模块中实现。
import UIKit
import Foundation
import XPModuleManager
public class ModuleA: NSObject, XPModuleDelegate {
public static let shared = ModuleA()
private override init() {
}
public func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
print("##### ModuleA didFinishLaunchingWithOptions")
return true
}
public func applicationDidEnterBackground(_ application: UIApplication) {
print("##### ModuleA applicationDidEnterBackground")
}
}
XPModuleManager
注册依赖AppDelegate的各个模块。并实现UIApplicationDelegate协议中对各个模块的回调。
Demo中仅实现了部分方法。
import Foundation
import UIKit
public final class XPModuleManager: NSObject, XPModuleDelegate {
public static let shared = XPModuleManager()
public func registerModule(_ module: XPModuleDelegate) {
if !modules.contains(where: { $0 === module }) {
modules.append(module)
}
}
public func unregisterModule(_ module: XPModuleDelegate) {
modules.removeAll(where: { $0 === module })
}
public func unregisterAllModules() {
modules = []
}
public func allModules() -> [XPModuleDelegate] {
return modules
}
private var modules: [XPModuleDelegate] = []
private override init() {
super.init()
}
}
// MARK: - Initializing the App
extension XPModuleManager {
public func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
if let instance = application.delegate as? UNUserNotificationCenterDelegate {
UNUserNotificationCenter.current().delegate = instance
}
for module in modules {
if module.responds(to: #selector(application(_:didFinishLaunchingWithOptions:))) {
_ = module.application?(application, didFinishLaunchingWithOptions: launchOptions)
}
}
return true
}
}
// MARK: - Responding to App Life-Cycle Events
extension XPModuleManager {
public func applicationDidBecomeActive(_ application: UIApplication) {
for module in modules {
if module.responds(to: #selector(applicationDidBecomeActive(_:))) {
module.applicationDidBecomeActive?(application)
}
}
}
public func applicationWillResignActive(_ application: UIApplication) {
}
public func applicationDidEnterBackground(_ application: UIApplication) {
for module in modules {
if module.responds(to: #selector(applicationDidEnterBackground(_:))) {
module.applicationDidEnterBackground?(application)
}
}
}
public func applicationWillEnterForeground(_ application: UIApplication) {
}
public func applicationWillTerminate(_ application: UIApplication) {
}
}
使用
在项目中注册各个模块
import Foundation
import XPModuleManager
import ModuleA
import ModuleB
class ProcessModule {
static func registerAllModules() {
XPModuleManager.shared.registerModule(ModuleA.shared)
XPModuleManager.shared.registerModule(ModuleB.shared)
}
}
AppDelegate.m
此时的AppDelegate.m。增删模块不需要修改AppDelegate.m。实现代码解耦
import UIKit
import XPModuleManager
@main
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
ProcessModule.registerAllModules()
_ = XPModuleManager.shared.application(application, didFinishLaunchingWithOptions: launchOptions)
return true
}
func applicationDidBecomeActive(_ application: UIApplication) {
XPModuleManager.shared.applicationDidBecomeActive(application)
}
func applicationDidEnterBackground(_ application: UIApplication) {
XPModuleManager.shared.applicationDidEnterBackground(application)
}
}