For logging purposes, you don't need to subclass or tediously add code to every UIViewController instance. Instead, swizzle the UIViewController's viewDidAppear method with your own.
private let swizzling: (AnyClass, Selector, Selector) -> () = { forClass, originalSelector, swizzledSelector in
let originalMethod = class_getInstanceMethod(forClass, originalSelector)
let swizzledMethod = class_getInstanceMethod(forClass, swizzledSelector)
method_exchangeImplementations(originalMethod!, swizzledMethod!)
}
extension UIViewController {
static let classInit: Void = {
let originalSelector = #selector(viewDidAppear(_:))
let swizzledSelector = #selector(swizzledViewDidAppear(_:))
swizzling(UIViewController.self, originalSelector, swizzledSelector)
}()
@objc func swizzledViewDidAppear(_ animated: Bool) {
print("Add your logging logic here")
// Call the original viewDidAppear - using the swizzledViewDidAppear signature
swizzledViewDidAppear(animated)
}
}
Note you'll have to kick-off the swizzle by overriding the AppDelegate's init for Swift 4.
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
override init() {
super.init()
UIViewController.classInit
}
Original credits to @efremidze and @TikhonovAlexander