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

跟踪iOS应用程序的任何用户交互,ios,swift,analytics,interaction,Ios,Swift,Analytics,Interaction,如何跟踪或捕获iOS应用程序的每个用户交互?就像按UIButton,UIBarButton。。。任何UIControl元素 我知道有数百种分析工具,如Google analytics、Flurry、Appsee等,但我想将这些数据保存在我自己的服务器上 Hello@tuvok如果你不想使用Google analytics和其他库,那么你必须制作一个API(Web服务)。您必须在每个用户交互上点击api,如按下按钮或其他任何操作。您可以将UIApplication子类化: 创建UIApplica

如何跟踪或捕获iOS应用程序的每个用户交互?就像按UIButton,UIBarButton。。。任何UIControl元素


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

Hello@tuvok如果你不想使用Google analytics和其他库,那么你必须制作一个API(Web服务)。您必须在每个用户交互上点击api,如按下按钮或其他任何操作。

您可以将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>>
func sendAction(action:Selector,to target:Any?,from sender:Any?,for event:UIEvent?)->Bool{
如果(发送方为UIButton){
打印(“操作:\(NSStringFromSelector(操作))-\(目标)-\(发送方)”)
}
返回super.sendAction(操作,收件人:目标,发件人:发件人,for:事件)
}
2017-07-08 14:46:18.270 UIApplications子类[94764:c07]操作:操作:--
2017-07-08 14:46:27.378 UIApplications子类[94764:c07]操作:操作:--

对于objective-c参考

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

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中的初始化:

    @UIApplicationMain 类AppDelegate:UIResponder、UIApplicationLegate{

    override init() {
        super.init()
        UIControl.classInit
    }
    

  • 示例如下:

    您可以在单个文本文件中编写每个操作!将该文件保存在文档目录中。然后在每次应用程序启动时将该文件发送到您的服务器(使用API)!