2

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

  1. 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.

  2. 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?

Abhishek
  • 1,654
  • 2
  • 18
  • 31

1 Answers1

0

sorry for the late response, I found myself in the same scenario, I was working on the library that tracks the user interactions and records it.

The approach I went on with the UIViewController's events especially covering the interactable components inside was to divide the UIViewController use cases.

In my case, most of the screens (UIViewControllers) were having:

  1. UIScrollView
  2. UITableView
  3. Components added directly without the need of the above two mentioned
  • For (1 & 3), I had to swizzle the UIViewConrtoller's viewDidAppear method, I do realize there are drawbacks for swizzling, but there is indeed a way that can get the previously registered method implementation and play with it. That way we can make sure that it runs fine even if there is some other SDK like firebase analytics making use of swizzling. Also I traversed the UIViewController views by accessing the child views and adding my own "Target action" for every component I found along the way during traversing, that inherits from UIControl.

  • For 2, I simply used the above extension technique for "UITableViewCell", and in the "awakeFromNib", simply added a Touch gesture on to the cell, and also attached Target Actions for any of the Interactable Component inside (any component that inherits from UIControl).

PS: I know some components do not directly inherit from UIControl, I had to cover them separately, like UITextView.

I would love to get in touch with you and share my insights on the topic. I am currently working on the Library. Ping me whenever you want.

rana5ohaib
  • 146
  • 4