PluggableAppDelegate
一个轻量级、以服务为中心的iOS ApplicationDelegate,用Swift编写,基于Fernando Martín Ortiz的想法(他的仓库现在不再支持)。
AppDelegate
是传统上不好的代码示例。许多执行不同任务的代码行被放在一起,形成了在应用程序生命周期内调用的方法。但这些担忧都结束了。
使用 PluggableAppDelegate
,您将 AppDelegate
与可以连接到它的服务分开。每个 ApplicationService
都有自己的生命周期,它与 AppDelegate
共享。
要求
- iOS 9.0+
- Xcode 10.2+
- Swift 5.0
使用
以下是一个 ApplicationService
的示例
import Foundation
import PluggableAppDelegate
final class LoggerApplicationService: NSObject, ApplicationService {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
print("🎉 LoggerApplicationService has started!")
return true
}
func applicationDidEnterBackground(_ application: UIApplication) {
print("🙀 LoggerApplicationService has entered background")
}
func applicationWillEnterForeground(_ application: UIApplication) {
print("😻 LoggerApplicationService has entered foreground")
}
}
这就完了。它与 AppDelegate 完全相同。将 ApplicationService
视为子 AppDelegate。
在 AppDelegate
中,您只需要继承 PluggableApplicationDelegate
并注册服务。
import UIKit
import PluggableAppDelegate
@UIApplicationMain
class AppDelegate: PluggableApplicationDelegate {
override var services: [ApplicationService] {
return [
RootVCApplicationService(),
LoggerApplicationService()
]
}
}
安装
CocoaPods
您可以使用 CocoaPods。
platform :ios, '9.0'
use_frameworks!
target 'MyApp' do
pod 'PluggableAppDelegate'
end
Carthage
您可以使用 Carthage。在 Cartfile 中指定。
github "pchelnikov/PluggableAppDelegate"
运行 carthage
编译框架,然后将构建的 MarkerKit.framework 拖入您的 Xcode 项目。请遵循 编译说明。
贡献
- 如果您发现了一个错误,可以在 这里打开一个问题。
- 如果您有功能请求,可以在 创建一个 pull request。
作者
Michael Pchelnikov
您可以在 Twitter 上关注我,账号为 @pchelnikov
致谢
许可证
PluggableAppDelegate 以 MIT 许可协议提供。更多信息请查看 LICENSE 文件。