如何知道您的应用程序是活动的还是空闲的?
How to know our application is active or idle?
我想在 "x" 分钟后在我的应用程序中设置自动超时方案。我尝试使用计划定时器调用定时器(在 "x" 分钟后)。用户正在阅读状态为 scrollable/tap 的 T&C 页面,同时触发了计时器。但是当用户不执行任何操作(触摸、滚动)时,我期待计时器调用。简单地说,如果用户在指定的时间段内没有触摸屏幕,我需要调用超时弹出窗口。我想为我的整个应用程序处理这种超时情况。
您可以在接收屏幕触摸的 UIWindow
上使用 UITapGestureRecognizer
。
在这里我添加了 5 秒的计时器,超时函数将调用。
在Appdelegate
中添加UIGestureRecognizerDelegate
var window: UIWindow?
var timer: Timer?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let tapGesture = UITapGestureRecognizer(target: self, action: nil)
tapGesture.delegate = self
window?.addGestureRecognizer(tapGesture)
//Start timer for first time
timer = Timer.scheduledTimer(timeInterval: 5, target: self, selector: #selector(self.timeOut), userInfo: nil, repeats: false)
return true
}
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
print(" tapped on screen,")
timer?.invalidate()
//set Timer Ex: 5 Second
//Run timer on touch
timer = Timer.scheduledTimer(timeInterval: 5, target: self, selector: #selector(self.update), userInfo: nil, repeats: false)
return false
}
@objc func timeOut() {
print("TimeOut")
}
我想在 "x" 分钟后在我的应用程序中设置自动超时方案。我尝试使用计划定时器调用定时器(在 "x" 分钟后)。用户正在阅读状态为 scrollable/tap 的 T&C 页面,同时触发了计时器。但是当用户不执行任何操作(触摸、滚动)时,我期待计时器调用。简单地说,如果用户在指定的时间段内没有触摸屏幕,我需要调用超时弹出窗口。我想为我的整个应用程序处理这种超时情况。
您可以在接收屏幕触摸的 UIWindow
上使用 UITapGestureRecognizer
。
在这里我添加了 5 秒的计时器,超时函数将调用。
在Appdelegate
UIGestureRecognizerDelegate
var window: UIWindow?
var timer: Timer?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let tapGesture = UITapGestureRecognizer(target: self, action: nil)
tapGesture.delegate = self
window?.addGestureRecognizer(tapGesture)
//Start timer for first time
timer = Timer.scheduledTimer(timeInterval: 5, target: self, selector: #selector(self.timeOut), userInfo: nil, repeats: false)
return true
}
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
print(" tapped on screen,")
timer?.invalidate()
//set Timer Ex: 5 Second
//Run timer on touch
timer = Timer.scheduledTimer(timeInterval: 5, target: self, selector: #selector(self.update), userInfo: nil, repeats: false)
return false
}
@objc func timeOut() {
print("TimeOut")
}