简体   繁体   中英

How to dismiss viewcontroller in appdelegate?

I create launchScreen for pause view like this.

func applicationWillResignActive(_ application: UIApplication) {
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let launchScreen = storyboard.instantiateViewController(withIdentifier: "launchScreen")
        launchScreen.restorationIdentifier = "launchScreen"

        var rootViewController = UIApplication.shared.keyWindow?.rootViewController
        while let presentController = rootViewController?.presentedViewController {
            rootViewController = presentController
        }
        rootViewController?.present(launchScreen, animated: false, completion: nil)
    }

func applicationDidEnterBackground(_ application: UIApplication) {

            guard let passcodeManageView = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "passcodeManageView") as? PasscodeManageViewController else { return }
            passcodeManageView.state = State.loginMode
            passcodeManageView.modalPresentationStyle = .overFullScreen

            var rootViewController = UIApplication.shared.keyWindow?.rootViewController
            while let presentController = rootViewController?.presentedViewController {
                rootViewController = presentController
            }
            rootViewController?.present(passcodeManageView, animated: false, completion: nil)

    }

But, How to dismiss launchScreen in applicationDidEnterBackground(:_)??

How can I find specific view controller and that dismiss??

According to Apple document for applicationDidEnterBackground(_:)`

Use this method to release shared resources, invalidate timers, and store enough app state information to restore your app to its current state in case it is terminated later. You should also disable updates to your app's user interface and avoid using some types of shared system resources (such as the user's contacts database). It is also imperative that you avoid using OpenGL ES in the background.

You shouldn't dismiss launch screen after app entered background. But if you still want to achieve it, use window?.rootViewController? to dismiss because at this time, window?.rootViewController? is launch screen

func applicationDidEnterBackground(_ application: UIApplication) {
  if (window?.rootViewController?.isKind(of: YOUR_LAUNCH_SCREEN_CLASS.self))! {
    window?.rootViewController?.dismiss(animated: true, completion: nil)
  }
}

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