Ios 静止5分钟后自动充电
在发布之前,我确实在谷歌上搜索过,但没有找到答案。我想弹出一个警报后5分钟的不活动和自动如果用户空闲。但在Swift中,我找不到任何指南或资源。有谁能为如何实现这一目标提供一些指导Ios 静止5分钟后自动充电,ios,swift,Ios,Swift,在发布之前,我确实在谷歌上搜索过,但没有找到答案。我想弹出一个警报后5分钟的不活动和自动如果用户空闲。但在Swift中,我找不到任何指南或资源。有谁能为如何实现这一目标提供一些指导 import UIKit class ViewController: UIViewController { var timer = Timer() override func viewDidLoad() { super.viewDidLoad() timer = Timer.scheduledT
import UIKit
class ViewController: UIViewController {
var timer = Timer()
override func viewDidLoad() {
super.viewDidLoad()
timer = Timer.scheduledTimer(timeInterval: 300, target: self, selector: #selector(userIsInactive), userInfo: nil, repeats: true)
let timerGesture = UITapGestureRecognizer(target: self, action: #selector(resetTimer))
self.view.isUserInteractionEnabled = true
self.view.addGestureRecognizer(timerGesture)
}
@objc func userIsInactive() {
// Alert user
let alert = UIAlertController(title: "You have been inactive for 5 minutes. We're going to log you off for security reasons.", message: nil, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Okay", style: .default, handler: { (UIAlertAction) in
//Log user off
}))
present(alert, animated: true)
timer.invalidate()
}
@objc func resetTimer() {
print("Reset")
timer.invalidate()
timer = Timer.scheduledTimer(timeInterval: 300, target: self, selector: #selector(userIsInactive), userInfo: nil, repeats: true)
}
}
希望这有帮助 但我需要把它添加到每个视图中?是否有任何方法可以一次性完成此操作?@L.William-当应用程序仅在后台运行时,还是当应用程序正在使用@Anbu时,您希望在其他情况下执行此操作。Karthik@L.William-请参阅此以获取帮助: