Last active
July 28, 2020 20:40
-
-
Save dangthaison91/a4bd9fdd566a13a98db6ef6dd02823e8 to your computer and use it in GitHub Desktop.
swizzle
This file contains 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 OSLog { | |
static let log = OSLog(subsystem: "", category: "Log") | |
} | |
extension UIViewController { | |
public class func swizzleTracing() { | |
// make sure this isn't a subclass | |
if self !== UIViewController.self { | |
return | |
} | |
struct Inner { | |
static let shared: () = { | |
let originalSelector = #selector(UIViewController.viewDidLoad) | |
let swizzledSelector = #selector(UIViewController.traceViewDidLoad) | |
guard | |
let originalMethod = class_getInstanceMethod(UIViewController.self, originalSelector), | |
let swizzledMethod = class_getInstanceMethod(UIViewController.self, swizzledSelector) | |
else { return } | |
let didAddMethod = class_addMethod(UIViewController.self, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod)) | |
if didAddMethod { | |
class_replaceMethod(UIViewController.self, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod)) | |
} else { | |
method_exchangeImplementations(originalMethod, swizzledMethod); | |
} | |
}() | |
} | |
let _ = Inner.shared | |
} | |
// MARK: - Method Swizzling | |
@objc func traceViewDidLoad() { | |
let viewName = self.description | |
os_log("Hook viewDidLoad BEGIN", log: OSLog.log, viewName) | |
self.traceViewDidLoad() | |
os_log("Hook viewDidLoad END", log: OSLog.log, viewName) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment