Created
May 25, 2020 06:54
-
-
Save levantAJ/8000323fd701107360e494f357184743 to your computer and use it in GitHub Desktop.
Swizzle UIApplication openURL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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