如何知道我们的应用程序是活动还是空闲



我想在"x"分钟后在我的应用程序中设置自动超时方案。我尝试使用计划计时器调用计时器(在"x"分钟后(。用户正在阅读具有可滚动/点击状态的条款和条件页面,计时器也会被触发。但是我期待当用户不执行任何操作(触摸,滚动(时进行计时器调用。只是如果用户在指定的时间段内没有触摸屏幕,我需要调用超时弹出窗口。我想为我的整个应用程序处理此超时情况。

您可以在屏幕上接收触摸的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")
    }

相关内容

  • 没有找到相关文章