在iOS中检测非活动状态(无用户交互)以显示单独的屏幕(例如叠加层)

时间:2018-11-20 09:08:43

标签: ios iphone swift navigation screensaver

如何在iOS App中检测不活动或没有用户交互。用于显示单独的屏幕(例如叠加层)或屏幕保护程序。     通过以下链接 link that used to present screen overlay when the app became inactive 我可以在应用程序中显示屏幕保护程序。但是使用正常的关闭视图控制器代码无法关闭显示的视图控制器。

import Homepage from "./pages/Homepage";
import Dashboard from "./pages/Dashboard";
import Leaderboard from "./pages/Leaderboard";
import Signup from "./pages/Signup";
import Login from "./pages/Login";
import About from "./pages/About";
import Quiz from "./pages/Quiz";
import Locked from "./containers/Locked";

import WebSocketService from "./services/WebSocketService";

// Add the redux.dispatch function to WebSocketService
WebSocketService.setDispatch(store.dispatch);

ReactDOM.render(
  <Provider store={store}>
    <Router>
      <Switch>
        <Route exact path="/" component={Homepage} />
        <Route exact path="/signup" component={Signup} />
        <Route exact path="/login" component={Login} />
        <Route exact path="/about" component={About} />
        {/* Locked */}
        <Locked>
          <Route exact path="/leaderboard" component={Leaderboard} />
          <Route exact path="/dashboard" component={Dashboard} />
          <Route exact path="/play/:quizId" component={Quiz} />
        </Locked>
        {/* 404 */}
        <Redirect to="/" />
      </Switch>
    </Router>
  </Provider>,
  document.getElementById("root")
);

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

1 个答案:

答案 0 :(得分:1)

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

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)



            }

        }

    }

}

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