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