在iOS中检测非活动状态(无用户交互)以显示单独的屏幕(例如叠加层) [英] Detecting inactivity (no user interaction) in iOS for showing separate Screen like an Overlay

查看:471
本文介绍了在iOS中检测非活动状态(无用户交互)以显示单独的屏幕(例如叠加层)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在iOS App中检测不活动或没有用户交互.用于显示单独的屏幕(例如叠加层)或屏幕保护程序. 通过以下链接 用于在应用程序变为非活动状态时显示屏幕覆盖的链接 我可以在应用程序中显示屏幕保护程序.但是使用普通的关闭视图控制器代码无法关闭显示的视图控制器.

How to detect inactivity or no user interaction in iOS App. For showing separate Screen like an Overlay or something like a screensaver. With the following link link that used to present screen overlay when the app became inactive I'm able to show the screen saver in the app. But with normal dismiss view controller code not being able to dismiss the presented view controller.

 we are presenting the custom overlay using

// The callback for when the timeout was fired.
    func applicationDidTimout(notification: NSNotification) {

       let storyboard = UIStoryboard(name: "MyStoryboard", bundle: nil)
              let vc = 
 storyboard.instantiateViewControllerWithIdentifier("myStoryboardIdentifier")
        UIApplication.shared.keyWindow?.rootViewController = vc       
    }

这是我们面临的问题.呈现视图后,我们将无法解雇或删除视图控制器.

推荐答案

来自@Prabhat Kesara的评论和@Vanessa Forney在帮助程序链接中的回答.我提供以下解决方案,它工作正常.如果有人提出这样的要求,他们可以使用此

From @Prabhat Kesara's comment and @Vanessa Forney answer in the helper link. I come with the below solution and it is working fine. If someone comes with the requirement like this they can use this

import UIKit
import Foundation

extension NSNotification.Name {
    public static let TimeOutUserInteraction: NSNotification.Name = NSNotification.Name(rawValue: "TimeOutUserInteraction")
}

class UserInterractionSetup: UIApplication {

    static let ApplicationDidTimoutNotification = "AppTimout"

    // The timeout in seconds for when to fire the idle timer.

    let timeoutInSeconds: TimeInterval = 1 * 60 //5 * 60 //

    var idleTimer: Timer?

    // Listen for any touch. If the screen receives a touch, the timer is reset.

    override func sendEvent(_ event: UIEvent) {

        super.sendEvent(event)


        if idleTimer != nil {

            self.resetIdleTimer()

        }

        if let touches = event.allTouches {

            for touch in touches {

                if touch.phase == UITouch.Phase.began {

                    self.resetIdleTimer()

                }

            }

        }

    }


    // Resent the timer because there was user interaction.

    func resetIdleTimer() {

        if let idleTimer = idleTimer {

            // print("1")

            let root = UIApplication.shared.keyWindow?.rootViewController

            root?.dismiss(animated: true, completion: nil)

            idleTimer.invalidate()

        }


        idleTimer = Timer.scheduledTimer(timeInterval: timeoutInSeconds, target: self, selector: #selector(self.idleTimerExceeded), userInfo: nil, repeats: false)

    }

    // If the timer reaches the limit as defined in timeoutInSeconds, post this notification.

    @objc func idleTimerExceeded() {

        print("Time Out")
         NotificationCenter.default.post(name:Notification.Name.TimeOutUserInteraction, object: nil)

        let screenSaverVC = UIStoryboard(name:"ScreenSaver", bundle:nil).instantiateViewController(withIdentifier:"LandingPageViewController") as! LandingPageViewController

        if let presentVc = TopMostViewController.sharedInstance.topMostViewController(controller: screenSaverVC){


            let root: UINavigationController = UIApplication.shared.keyWindow!.rootViewController as! UINavigationController

            if let topView = root.viewControllers.last?.presentedViewController {

                topView.dismiss(animated: false) {

                    root.viewControllers.last?.navigationController?.present(presentVc, animated: true, completion: nil)

                }

            }else if let topViewNavigation = root.viewControllers.last {

                topViewNavigation.navigationController?.present(presentVc, animated: true, completion: nil)



            }

        }

    }

}

我遇到的困难是在已经显示的视图上方显示覆盖或屏幕保护程序.在这种情况下,我们也在上述解决方案中进行处理. 编码愉快:)

the difficulty I was faced was presenting overlay or screensaver above an already presented a view. That case, also we are handling in the above solution. Happy coding :)

这篇关于在iOS中检测非活动状态(无用户交互)以显示单独的屏幕(例如叠加层)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆