简体   繁体   中英

Perform segue from AppDelegate

I need to perform a segue from the AppDelegate when the user has left the app in the background for more than three minutes. This is my code:

func applicationWillEnterForeground(_ application: UIApplication) {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
    let preferences = UserDefaults.standard
    let time_in = preferences.object(forKey: "time_background") as! Date
    let timenow = Date().addingTimeInterval(-3*60) as Date
    if time_in <= timenow {
        print("timeout")
        //transition to the login
    }
    print(preferences.object(forKey: "session_time") ?? "test")
}

So at //transition to the login I want the segue to bring the user back to the login page.

But I don't understand how to do a segue from the AppDelegate.

I dont think attempting to use a segue would be the right way to go about this. firstly you would need to get the current root view controller that is displayed and push your login view controller onto the stack.

I think your best option would be to set the root view controller to your login view controller and then when the user authenticates you set the root view controller to be the navigation controller for your application. This will reset the view hierarchy/stack and will prevent issues with memory, hiding back buttons so the user can't just skip the login VC etc

In my application I have two custom navigation controllers. One for authentication related vc's and one for the main application. I just swap out the window.rootViewController as and when needed

In my AppDelegate I have

self.window = UIWindow(frame: UIScreen.main.bounds)

if authenticationManager.authenticated() {
    self.initialiseMainStack()
} else {
    self.initialiseAuthenticationStack()
}

self.window?.makeKeyAndVisible()

and then a little later down the file..

func initialiseMainStack() {
    let nav = UINavigationController()
    let vc = MainViewController()
    nav.setViewControllers([vc])
    self.window.rootViewController = nav
}

func initialiseAuthenticationStack() {
    let nav = UINavigationController()
    let vc = AuthViewController()
    nav.setViewControllers([vc])
    self.window.rootViewController = nav
}
func showLoginScreen(){
    let loginVC = UIStoryboard.getMainStoryboard().instantiateViewController(withIdentifier: "LoginViewControllerIdentifier") as! LoginViewController

    UIApplication.shared.delegate!.window!!.rootViewController = loginVC     
}

extension UIStoryboard{
    //returns storyboard from default bundle if bundle paased as nil.
    public class func getMainStoryboard() -> UIStoryboard{
        return UIStoryboard(name: "Main", bundle: nil)
    }
}

Either you could save a reference to the currently displayed viewcontroller and the perform the segue on that one.

Or you could just replace the window with the LoginViewController .

window?.rootViewController = loginViewController

我不知道您的应用程序结构有多困难,但是您可以创建一个观察器并将通知发送到每个相关的实例化viewcontroller,您可以从中导航用户以登录屏幕。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM