All Projects → kealdishx → SwiftLoadHook

kealdishx / SwiftLoadHook

Licence: MIT license
Use a hack way to achieve similar functions as Load() or initialize() in OC

Programming Languages

swift
15916 projects

Projects that are alternatives of or similar to SwiftLoadHook

web
React hooks done right, for browser and SSR.
Stars: ✭ 940 (+4376.19%)
Mutual labels:  hook
usehooks-ts
React hook library, ready to use, written in Typescript.
Stars: ✭ 2,873 (+13580.95%)
Mutual labels:  hook
svg-to-swiftui-core
Headless package for converting SVG to SwiftUI
Stars: ✭ 25 (+19.05%)
Mutual labels:  hook
react-use-downloader
Creates a download handler function and gives progress information
Stars: ✭ 65 (+209.52%)
Mutual labels:  hook
riru MPH
Hook android system prop function to add properties
Stars: ✭ 23 (+9.52%)
Mutual labels:  hook
KeyBoardTool
Keyboard key detection software realized by Qt(Qt实现的键盘按键检测软件)
Stars: ✭ 35 (+66.67%)
Mutual labels:  hook
react-transition-state
Zero dependency React transition state machine.
Stars: ✭ 239 (+1038.1%)
Mutual labels:  hook
BaiDuYunCrack
iOS百度云盘 破解速度限制、去广告、去更新 无需越狱~
Stars: ✭ 82 (+290.48%)
Mutual labels:  hook
react-eyedrop
Seamlessly integrate a static typed, fully-tested color-picking React component/hook!
Stars: ✭ 17 (-19.05%)
Mutual labels:  hook
nxdk-rdt
Remote Dev Tool is a tool to remote control an Xbox using memory access and RPC
Stars: ✭ 23 (+9.52%)
Mutual labels:  hook
resize-observer-hook
⚛️ A React Hook to monitor changes in the size of an element using native ResizeObserver API 🔍
Stars: ✭ 45 (+114.29%)
Mutual labels:  hook
iosHookViewId
A solution for ios hook view id(给iOS应用自动生成控件id)
Stars: ✭ 44 (+109.52%)
Mutual labels:  hook
subhook.nim
subhook wrapper for Nim https://github.com/Zeex/subhook
Stars: ✭ 15 (-28.57%)
Mutual labels:  hook
messier
Messier is an app for tracing objective-c methods in an iOS app.
Stars: ✭ 72 (+242.86%)
Mutual labels:  hook
Uatu
Android方法调用跟踪 ; 方法耗时统计 ; 方法调用参数以及返回值跟踪 ; 方法调用替换;方法hook
Stars: ✭ 93 (+342.86%)
Mutual labels:  hook
use-color-change
📈📉React hook for flashing a text when a value becomes higher or lower
Stars: ✭ 32 (+52.38%)
Mutual labels:  hook
objects-hooks-remover
Package to remove WordPress hook callbacks that uses object methods or closures.
Stars: ✭ 44 (+109.52%)
Mutual labels:  hook
react-hook-videojs
Easy React integration of Video.js using hooks.
Stars: ✭ 37 (+76.19%)
Mutual labels:  hook
klyva
A state management library that follows the React component model
Stars: ✭ 53 (+152.38%)
Mutual labels:  hook
View-Load-ReTry
这个加载框架有点不一样,针对View进行加载,加载页面还保持了原View的属性,侧重点在灵活,哪里需要加载哪里,加载状态页面完全自定义,无任何限制,针对加载结果可以按需配置对应页面,LeakCanary检测无内存泄漏
Stars: ✭ 116 (+452.38%)
Mutual labels:  load

SwiftLoadHook 中文介绍

Purpose

This lib uses a hack way to achieve similar functions as Load() or initialize().

Reason

Upon migrating a project to Swift 3.1, Xcode raises a warning:

Method ‘initialize()’ defines Objective-C class method ‘initialize’, which is not guaranteed to be invoked by Swift and will be disallowed in future versions.

Requirements

  • iOS 8.0+
  • swift 3.0+

Usage

First, drop files under Sources folder to your project.

Then, your target class should conforms to SelfAware protocol, and implements the functions in SelfAware protocol.

Finally, write the code you want in awake function just like that in Load() or Initialize().

Example

This example is used to help you understand how to use, you can find the code in the files under Example folder. In this example, I want to swizzle the IMP of function viewWillAppear() in UIViewController.

First, UIViewController should conform to SelfAware protocol and implement functions of the protocol.

extension UIViewController: SelfAware {

  static func awake() {
    UIViewController.classInit()
  }

  static func classInit() {
    swizzleMethod
  }
}

Then, I should implement swizzleMethod function. while swizzling methods, we should use dispatch_once in Load() or Initialize() function. However, since swift 3.x, we cannot find dispatch_once in API. How can we handle this? We can use static let instance to handle with it.

@objc func swizzled_viewWillAppear(_ animated: Bool) {
    swizzled_viewWillAppear(animated)
    print("swizzled_viewWillAppear")
  }

  private static let swizzleMethod: Void = {
    let originalSelector = #selector(viewWillAppear(_:))
    let swizzledSelector = #selector(swizzled_viewWillAppear(_:))
    swizzlingForClass(UIViewController.self, originalSelector: originalSelector, swizzledSelector: swizzledSelector)
  }()

  private static func swizzlingForClass(_ forClass: AnyClass, originalSelector: Selector, swizzledSelector: Selector) {

    let originalMethod = class_getInstanceMethod(forClass, originalSelector)
    let swizzledMethod = class_getInstanceMethod(forClass, swizzledSelector)

    guard (originalMethod != nil && swizzledMethod != nil) else {
      return
    }

    if class_addMethod(forClass, originalSelector, method_getImplementation(swizzledMethod!), method_getTypeEncoding(swizzledMethod!)) {
      class_replaceMethod(forClass, swizzledSelector, method_getImplementation(originalMethod!), method_getTypeEncoding(originalMethod!))
    } else {
      method_exchangeImplementations(originalMethod!, swizzledMethod!)
    }
  }

Thanks

@JORDAN SMITH

Reference

Handling the Deprecation of initialize()

License

SwiftLoadHook is released under the MIT license. See LICENSE for details.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].