Skip to content

Instantly share code, notes, and snippets.

@levantAJ
Created May 25, 2020 06:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save levantAJ/8000323fd701107360e494f357184743 to your computer and use it in GitHub Desktop.
Save levantAJ/8000323fd701107360e494f357184743 to your computer and use it in GitHub Desktop.
Swizzle UIApplication openURL
extension UIApplication {
static let classInit: () -> () = {
swizzle(UIApplication.self, #selector(openURL(_:)), #selector(swizzledOpenURL(_:)))
swizzle(UIApplication.self, #selector(open(_:options:completionHandler:)), #selector(swizzledOpen(_:options:completionHandler:)))
}
@objc func swizzledOpenURL(_ url: URL) {
print(">>>>>url", url)
}
@objc func swizzledOpen(_ url: URL, options: [UIApplication.OpenExternalURLOptionsKey: Any], completionHandler completion: ((Bool) -> Void)?) {
print(">>>>>url", url)
}
}
private let swizzle: (AnyClass, Selector, Selector) -> () = { fromClass, originalSelector, swizzledSelector in
guard let originalMethod = class_getInstanceMethod(fromClass, originalSelector),
let swizzledMethod = class_getInstanceMethod(fromClass, swizzledSelector)
else { return }
method_exchangeImplementations(originalMethod, swizzledMethod)
}
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
UIApplication.classInit()
return true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment