Degu 0.2.0

Degu 0.2.0

rinovYusuke Morishitamarty-suzukiTaiki Suzukimarty-suzuki 维护。



Degu 0.2.0

  • marty-suzuki

DEGU

platforms Swift4.1 License
Carthage SwiftPM Version

如果您想获取应用的所有事件,您可以从 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 使这变得很容易!!

使用

1. ApplicationProxy

这是一个例子。您只需要实现 2 个实现。

  • 将代理设置为 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)")
}

2. 重写 load 或 initialize 方法

+(void)load+(void)initialize 在 Swift 中不允许。

您可以在 RuntimeHandler 的扩展 中重写 +(void)load+(void)initialize

extension RuntimeHandler {
    open override class func handleLoad() {
        // do something
    }

    open override class func handleInitialize() {
        // do something
    }
}

⚠️ handleLoadhandleInitialize 不会在 RuntimeHandler 的子类 中调用。它只在工作在 RuntimeHandler 的扩展 中。

安装

Carthage

如果您使用的是 Carthage,只需将 Degu 添加到您的 Cartfile

github "cats-oss/Degu"

CocoaPods

您可以通过 CocoaPods 安装 Degu。只需将以下行添加到您的 Podfile

pod 'Degu'

Swift Package Manager

如果您使用的是 Swift Package Manager,只需将 Degu 添加到您的 Package.swift

dependencies: [
    .package(url: "https://github.com/cats-oss/Degu", from: "0.1.0")
]

要求

  • Xcode 11.1
  • Swift 5.1

许可

Degu 使用 MIT 许可。更多信息请参阅 LICENSE 文件