ios - 跟踪用户浏览 iOS 应用的 UI 所花费的时间

标签 ios cocoa-touch time

我想测量用户在我的应用程序中执行某些操作需要多长时间(以秒为单位)。一些示例是登录、按下某个页面上的按钮等。

我正在使用 NSTimer为了那个原因。我从 viewDidLoad 开始的特定页面,并在我想要测量的点停止它。

我还想测量某些事情的累积时间。我想在登录屏幕上启动计时器,然后继续计时器,直到用户到达下一个 View Controller 并单击某个按钮。

我不知道该怎么做。应该在我的应用程序委托(delegate)中创建一个全局变量吗?还是有其他更好的方法?

最佳答案

不需要 NSTimer,您只需要记录开始时间并将它们与停止时间进行比较。尝试使用一个小助手类,例如:

class MyTimer {

    static let shared = MyTimer()

    var startTimes = [String : Date]()

    func start(withKey key: String) {
        startTimes[key] = Date()
    }

    func measure(key: String) -> TimeInterval? {
        if let start = startTimes[key] {
            return Date().timeIntervalSince(start)
        }

        return nil
    }

}

要使用它,只需在开始长时间运行的任务之前调用 start(withKey:) 即可。
    MyTimer.shared.start(withKey: "login")

做一些需要一段时间的事情,然后在完成后调用 measure(key:) 。因为 MyTimer 是一个单例,所以可以从代码中的任何位置调用它。
    if let interval = MyTimer.shared.measure("login") {
        print("Logging in time: \(interval)")
    }

如果您使用多个线程,您可以为此添加一些线程安全性,但它应该在简单的场景中正常工作。

关于ios - 跟踪用户浏览 iOS 应用的 UI 所花费的时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44767715/

相关文章:

iphone - 如何在使用 GPS 时减少 iPhone 电池消耗

iPhone 没有 Default-Portrait.png 和 Default-Landscape.png 文件?

python - 如何最好地处理列表或数据框中的日期时间?

excel - 按时间间隔运行宏的代码?

python - Pandas:时间转换器独立工作,但在读取 csv 文件时不工作

ios - 对 QLRemotePreviewContentController 的开始/结束外观转换的不平衡调用

ios - 隐藏导航栏导致 View 向上移动

ios - react 原生 : TypeError: null is not an object (evaluating 'SplashScreen.preventAutoHide' )

ios - 如何处理退款/取消应用内购买

cocoa-touch - 在 NSArray 中查找重复项