Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在swift中计算当前屏幕上的用户阅读活动?_Ios_Swift_Time - Fatal编程技术网

Ios 如何在swift中计算当前屏幕上的用户阅读活动?

Ios 如何在swift中计算当前屏幕上的用户阅读活动?,ios,swift,time,Ios,Swift,Time,我的应用程序中有一个任务 在Tableview中,我加载一些书籍列表, 当用户选择单元格时,我用PDF URL打开该书 我需要计算用户花在这本书上的阅读时间,我需要将这些时间发送到后端 假设用户打开书,我将启动计时器,我将如何观察用户阅读该书,或者假设将其放在后台 你知道如何在你的viewDidLoad()中校准时间吗。您可以像这样添加观察者: NotificationCenter.default.addObserver(self, selector: #selector(application

我的应用程序中有一个任务

在Tableview中,我加载一些书籍列表, 当用户选择单元格时,我用PDF URL打开该书 我需要计算用户花在这本书上的阅读时间,我需要将这些时间发送到后端

假设用户打开书,我将启动计时器,我将如何观察用户阅读该书,或者假设将其放在后台

你知道如何在你的
viewDidLoad()
中校准时间吗。您可以像这样添加观察者:

NotificationCenter.default.addObserver(self, selector: #selector(applicationWillResignActive(notification:)), name: UIApplication.willResignActiveNotification, object: UIApplication.shared)

NotificationCenter.default.addObserver(self, selector: #selector(didBecomeActive(notification:)), name: UIApplication.didBecomeActiveNotification, object: UIApplication.shared)
要检查用户何时进入后台,只需创建这两种方法

var start: CFAbsoluteTime!

//This method will run when user enters the background  
@objc func applicationWillResignActive(notification: NSNotification){
    start = CFAbsoluteTimeGetCurrent()
    print("Background entered")
}

//This method will run when user returned to the application
@objc func didBecomeActive(notification: NSNotification){
    let backgroundTime = CFAbsoluteTimeGetCurrent() - start
    print(backgroundTime)
    print("Returned to the application")
}