Skip to content

Hook AppDelegate methods ,distributed your UIApplicationDelegate code to every component.

License

Notifications You must be signed in to change notification settings

FengDeng/AppDelegateHooks

Repository files navigation

AppDelegateHooks

CocoaPods Compatible Carthage Compatible Platform

AppDelegateHooks: easy library to hook AppDelegate methods ,distributed your UIApplicationDelegate code in every component.

Features

  • Native UIApplicationDelegate code prompt
  • Just new class inhert AppDelegateHook
  • Rewrite level property,Custom calling sequence
  • Hooks everywhere

Installation

Cocoapods

pod 'AppDelegateHooks', '~> 0.0.1'

Carthage

github "FengDeng/AppDelegateHooks" ~> 0.0.1

SPM

dependencies: [
    .package(url: "https://github.com/FengDeng/AppDelegateHooks", from: "0.0.1")
]

Usage

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

Thanks for Aspects which developed by @steipete in GitHub

中文

AppDelegateHooks: 一个可以轻松拦截AppDelegate所有回调的轻量级的库,把你的初始化代码分散到各个组件内部。

特性

  • 原生的UIApplicationDelegate代码提示
  • 新建Class,继承AppDelegateHook即可,无需其他操作
  • 提供重写level,自定义调用优先级
  • 组件内,模块内,无限制hook主工程生命周期

安装

Cocoapods

pod 'AppDelegateHooks', '~> 0.0.1'

Carthage

github "FengDeng/AppDelegateHooks" ~> 0.0.1

SPM

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

About

Hook AppDelegate methods ,distributed your UIApplicationDelegate code to every component.

Resources

License

Stars

Watchers

Forks

Packages

No packages published