如果你想获取一个应用的所有事件,你可以从 UIApplication.sendEvent(_:)
中获取。但是,需要以下实现才能从其中获取事件。
/* MyApplication.swift */
class MyApplication: UIApplication {
override sendEvent(_ event: UIEvent) {
super.sendEvent(event)
print(event)
}
}
/* main.swift */
private let applicationClassName: String = {
#if DEBUG
return NSStringFromClass(MyApplication.self)
#else
return NSStringFromClass(UIApplication.self)
#endif
}()
UIApplicationMain(
CommandLine.argc,
UnsafeMutableRawPointer(CommandLine.unsafeArgv)
.bindMemory(
to: UnsafeMutablePointer<Int8>.self,
capacity: Int(CommandLine.argc)),
applicationClassName,
NSStringFromClass(AppDelegate.self)
)
/* AppDelegate.swift */
// @UIApplicationMain <- comment out
class AppDelegate: UIResponder, UIApplicationDelegate {
...
}
Degu
让这一切变得简单!!
这是一个例子。你只需要实现以下两点。
ApplicationProxy.shared.delegate
ApplicationProxyDelegate
import Degu
class AppDelegate: UIResponder, ApplicationDelegate {
...
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
#if DEBUG
ApplicationProxy.shared.delegate = self
#endif
...
return true
}
...
}
extension AppDelegate: ApplicationProxyDelegate {
func applicationProxy(_ proxy: ApplicationProxy, didSendEvent event: UIEvent) {
print(event)
}
}
此外,你可以获取所有 ViewControllers 的生命周期方法调用。
func applicationProxy(_ proxy: ApplicationProxy, didCallLifeCycle lifeCycle: ViewControllerLifeCycle, ofViewController viewController: UIViewController) {
print("ViewController = \(viewController)")
print("LifeCycle = \(lifeCycle)")
}
+(void)load 和 +(void)initialize 不允许在 Swift 中使用。
你可以在 RuntimeHandler 的 Extension 中重写 +(void)load
和 +(void)initialize
。
extension RuntimeHandler {
open override class func handleLoad() {
// do something
}
open override class func handleInitialize() {
// do something
}
}
handleLoad
和 handleInitialize
不会被 RuntimeHandler 的子类 调用。它只在 RuntimeHandler 的 Extension 中有效。
如果你使用 Carthage, 只需要添加 Degu 到你的 Cartfile
github "cats-oss/Degu"
Degu 可以通过 CocoaPods 获取。要安装它,只需要添加以下行到你的 Podfile
pod 'Degu'
如果你使用 Swift Package Manager,只需要添加 Degu 到你的 Package.swift
dependencies: [
.package(url: "https://github.com/cats-oss/Degu", from: "0.2.0")
]
Degu 在 MIT 许可证下可用。查看 LICENSE 文件 获取更多信息。