I am working on an analytics SDK which will track all the user events which view is appeared or disappeared, Which button is clicked, Which UISwitch is turned ON or OFF, UITableView is scrolled or cell is tapped etc.
I am using method swizzling for implementing this feature but I have seen some drawbacks related to this
If swizzling happens multiple times, either your code won’t work, or the firebase (or any other framework swizzling the same method) won’t work.
When newer iOS versions are released, there are chances that the swizzling fails. You may have to cross-check this every time.
I have read the drawback on this article
https://medium.com/@abhimuralidharan/method-swizzling-in-ios-swift-1f38edaf984f
Here is my sample code how I am doing to track 1. ViewDidAppeard tracking
@objc func viewDidDisappearOverride(_ animated: Bool) {
}
static func swizzleViewDidDisappear() {
if self != UIViewController.self {
return
}
let _: () = {
let originalSelector = #selector(UIViewController.viewDidDisappear(_:))
let swizzledSelector = #selector(UIViewController.viewDidDisappearOverride(_:))
guard let originalMethod = class_getInstanceMethod(self, originalSelector),
let swizzledMethod = class_getInstanceMethod(self, swizzledSelector) else { return }
method_exchangeImplementations(originalMethod, swizzledMethod)
}()
2. ButtonClick tracking with extension UIButton
override open func awakeFromNib() {
super.awakeFromNib()
self.addTarget(self, action: #selector(globalUIButonAction), for: .touchUpInside)
}
@objc func globalUIButonAction (_ sender: UIButton) { }
How to track all the events what is the best method or solution to do it?