简体   繁体   English

跟踪自定义分析框架 iOS 的 ViewController 和 UIControls 的所有事件

[英]Tracking All Events of ViewController and UIControls for Custom Analytics Framework iOS

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.我正在开发一个分析 SDK,它将跟踪视图出现或消失的所有用户事件,单击哪个按钮,打开或关闭哪个 UISwitch,滚动 UITableView 或点击单元格等。

I am using method swizzling for implementing this feature but I have seen some drawbacks related to this我正在使用方法 swizzling 来实现此功能,但我看到了与此相关的一些缺点

  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.如果 swizzling 发生多次,要么您的代码不起作用,要么 firebase(或任何其他 swizzling 相同方法的框架)将不起作用。

  2. When newer iOS versions are released, there are chances that the swizzling fails.当发布较新的 iOS 版本时,可能会出现 swizzling 失败的情况。 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 https://medium.com/@abhimuralidharan/method-swizzling-in-ios-swift-1f38edaf984f

Here is my sample code how I am doing to track 1. ViewDidAppeard tracking这是我如何跟踪 1. ViewDidAppeard 跟踪的示例代码

@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 2. ButtonClick 跟踪扩展 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?如何跟踪所有事件,最好的方法或解决方案是什么?

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.我继续处理 UIViewController 事件的方法,特别是涵盖内部可交互组件的方法是划分 UIViewController 用例。

In my case, most of the screens (UIViewControllers) were having:就我而言,大多数屏幕(UIViewControllers)都具有:

  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.对于 (1 & 3),我不得不对 UIViewConrtoller 的 viewDidAppear 方法进行 swizzle,我确实意识到 swizzling 有缺点,但确实有一种方法可以获取先前注册的方法实现并使用它。 That way we can make sure that it runs fine even if there is some other SDK like firebase analytics making use of swizzling.这样我们就可以确保它运行良好,即使有一些其他的 SDK,比如使用 swizzling 的 firebase 分析。 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.此外,我通过访问子视图并为我在遍历过程中发现的每个组件添加我自己的“目标操作”来遍历 UIViewController 视图,这些组件继承自 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).对于 2,我只是将上述扩展技术用于“UITableViewCell”,并在“awakeFromNib”中,简单地在单元格上添加了一个触摸手势,并为内部的任何可交互组件(任何继承自的组件)附加了目标操作UI控件)。

PS: I know some components do not directly inherit from UIControl, I had to cover them separately, like UITextView. PS:我知道有些组件不直接从 UIControl 继承,我不得不单独覆盖它们,比如 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.随时给我打电话。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM