跟踪iOS应用程序的任何用户交互

时间:2017-11-21 12:34:23

标签: ios swift analytics interaction

如何跟踪或捕捉iOS应用的每个用户互动?就像按下UIButton,UIBarButton,......任何UIControl元素一样。

我知道有数百种分析工具,例如Google Analytics,Flurry,Appsee等,但我想将这些数据保存在自己的服务器上。

3 个答案:

答案 0 :(得分:3)

Hello @tuvok如果您不想使用Google分析和其他库,那么您必须制作API(Web服务)。你必须在按下按钮或其他任何用户互动时点击api。

答案 1 :(得分:2)

您可以继承UIApplication:

  • 创建UIApplication子类
  • 覆盖了 sendAction(_ action:Selector,to target:Any?,from sender:Any?,for event:UIEvent?) 事件方法,记得调用超级实现
  • 在实现中放入NSLog或其他诊断代码

例如,每次按下UIButton时都会打印一条日志:

func sendAction(_ action: Selector, to target: Any?, from sender: Any?, for event: UIEvent?) -> Bool {
    if (sender is UIButton) {
        print("Action: \(NSStringFromSelector(action)) - \(target) - \(sender)")
    }
    return super.sendAction(action, to: target, from: sender, for: event)
}


2017-07-08 14:46:18.270 UIApplicationSubclass[94764:c07] Action: anAction: - <ViewController: 0x76790a0> - <UIRoundedRectButton: 0x767b9b0; frame = (103 66; 73 44); opaque = NO; autoresize = TM+BM; layer = <CALayer: 0x767bad0>>
2017-07-08 14:46:27.378 UIApplicationSubclass[94764:c07] Action: anAction: - <ViewController: 0x76790a0> - <UIRoundedRectButton: 0x767b9b0; frame = (103 66; 73 44); opaque = NO; autoresize = TM+BM; layer = <CALayer: 0x767bad0>>

对于objective-c参考click here

答案 2 :(得分:1)

找到了一个简单的解决方案:

1.创建一个UIControl扩展

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 UIControl {

    static let classInit: Void = {
        let originalSelector = #selector(sendAction(_:to:for:))
        let swizzledSelector = #selector(swizzled_sendAction(_:to:for:))
        swizzling(UIControl.self, originalSelector, swizzledSelector)
    }()

    @objc func swizzled_sendAction(_ action: Selector, to target: Any?, for event: UIEvent?) {

        swizzled_sendAction(action, to: target, for: event)
        print("action was triggered")
    }
}
    在AppDelegate中
  1. 初始化:

    @UIApplicationMain class AppDelegate:UIResponder,UIApplicationDelegate {

    override init() {
        super.init()
        UIControl.classInit
    }
    
  2. 示例来自:https://stackoverflow.com/a/42047289/2820043