0

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??

1 Answers1

3

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)
  }
}
trungduc
  • 11,926
  • 4
  • 31
  • 55
  • Did you get any problem with the solution? – trungduc Jul 11 '18 at 10:00
  • but...how can I remove specific view controller?? because in my case sometime view that should not disappear disappears. –  Jul 11 '18 at 10:01
  • @allanWay As I know, you want to dismiss launch screen if it's appeared. So just need to check class kind of `rootViewController`. If it has class of launch screen, dismiss it. – trungduc Jul 11 '18 at 10:05
  • your answer working good until not enter background. because I create another viewcontroller app enter background. –  Jul 11 '18 at 10:05
  • @allanWay You can check my updated answer and tell me it work or not :) – trungduc Jul 11 '18 at 10:06
  • I tried your update answer...but did not work... I'll update my code. –  Jul 11 '18 at 11:25
  • As I understand, whenever user hides app to background, you want to show a passcode screen. How about launch screen? You want to dismiss it before showing passcode or something else? Please explain clearer – trungduc Jul 11 '18 at 11:40
  • If the Home button is pressed twice, or the Control Center is launched, the app becomes inactive. At this time, I want to display the launch screen. However, if you press the Home button and the app reaches the background at all, I want to display the passcode view. –  Jul 11 '18 at 12:18
  • @allanWay In this case, I think my solution will work. All you need is moving code to present passcode to completion of dismiss method – trungduc Jul 11 '18 at 15:38