AppDelegateHooks: easy library to hook AppDelegate methods ,distributed your UIApplicationDelegate code in every component.
- Native UIApplicationDelegate code prompt
- Just new class inhert AppDelegateHook
- Rewrite level property,Custom calling sequence
- Hooks everywhere
pod 'AppDelegateHooks', '~> 0.0.1'
github "FengDeng/AppDelegateHooks" ~> 0.0.1
dependencies: [
.package(url: "https://github.com/FengDeng/AppDelegateHooks", from: "0.0.1")
]
sample in main project:
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
print("Main didFinishLaunchingWithOptions")
return true
}
}
in your workspace or framework:
class ExampleHook1 : AppDelegateHook{
//添加你想要的生命周期
self.level = 1000//如果你这个组件想要最先加载 level越大越先
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]?) -> Bool {
print("ExampleHook1 didFinishLaunchingWithOptions")
return true
}
func applicationWillResignActive(_ application: UIApplication) {
print("ExampleHook1 applicationWillResignActive")
}
......
}
class ExampleHook2 : AppDelegateHook{
//添加你想要的生命周期
self.level = 10000//如果你这个组件想要最先加载 level越大越先
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]?) -> Bool {
print("ExampleHook2 didFinishLaunchingWithOptions")
return true
}
func applicationWillResignActive(_ application: UIApplication) {
print("ExampleHook2 applicationWillResignActive")
}
......
}
print:
ExampleHook2 didFinishLaunchingWithOptions
ExampleHook1 didFinishLaunchingWithOptions
Main didFinishLaunchingWithOptions
ExampleHook2 applicationWillResignActive
ExampleHook1 applicationWillResignActive
Thanks for Aspects which developed by @steipete in GitHub
AppDelegateHooks: 一个可以轻松拦截AppDelegate所有回调的轻量级的库,把你的初始化代码分散到各个组件内部。
- 原生的UIApplicationDelegate代码提示
- 新建Class,继承AppDelegateHook即可,无需其他操作
- 提供重写level,自定义调用优先级
- 组件内,模块内,无限制hook主工程生命周期
pod 'AppDelegateHooks', '~> 0.0.1'
github "FengDeng/AppDelegateHooks" ~> 0.0.1
dependencies: [
.package(url: "https://github.com/FengDeng/AppDelegateHooks", from: "0.0.1")
]
在子组件或者模块内新建文件
class ExampleHook1 : AppDelegateHook{
//添加你想要的生命周期
self.level = 1000//如果你这个组件想要最先加载 level越大越先
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]?) -> Bool {
print("ExampleHook1 didFinishLaunchingWithOptions")
return true
}
func applicationWillResignActive(_ application: UIApplication) {
print("ExampleHook1 applicationWillResignActive")
}
......
}
class ExampleHook2 : AppDelegateHook{
//添加你想要的生命周期
self.level = 10000//如果你这个组件想要最先加载 level越大越先
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]?) -> Bool {
print("ExampleHook2 didFinishLaunchingWithOptions")
return true
}
func applicationWillResignActive(_ application: UIApplication) {
print("ExampleHook2 applicationWillResignActive")
}
......
}
打印如下:
ExampleHook2 didFinishLaunchingWithOptions
ExampleHook1 didFinishLaunchingWithOptions
Main didFinishLaunchingWithOptions
ExampleHook2 applicationWillResignActive
ExampleHook1 applicationWillResignActive