Search code examples
iosswiftiphonexcodestoryboard

Presenting lock screen on top of main storyboard


When a user is entering the app either from didFinishLaunchingWithOptions or applicationWillEnterForeground, I would like to display a lock screen.

I have the lock screen set up in another storyboard, not on the main one.

If I instantiate the lock screen storyboard and then move to the main storyboard, the app will not keep the same state of when the user left the app, when entering the app with applicationWillEnterForeground.

So if the user is in a specific view controller and then leave the app and enter again with applicationWillEnterForeground, the storyboard will be instantiated as the lock screen storyboard and once the user will unlock the screen he will be redirected to the rootViewController of the main storyboard, and not to the view controller he was at before.

The main storyboard starts with a UITabBarController.

How do you overcome this?

if let lockedViewController = UIStoryboard(name: "Locked", bundle: .main).instantiateInitialViewController() {

    window?.rootViewController = lockedViewController
    window?.makeKeyAndVisible()
}

Lock screen storyboard:

enter image description here

Thanks


Solution

  • Just create new instance of UIWindow and keep reference in your appdelegate.

    On foreground transition create new window and on unlock release window

    private var lockWindow:UIWindow?
    func createNewWindow()  {
        lockWindow = UIWindow(frame: UIScreen.main.bounds)
        lockWindow!.rootViewController = UIStoryboard(name: "Locked", bundle: .main).instantiateInitialViewController()
        lockWindow!.windowLevel = UIWindow.Level.alert + 1;
        lockWindow!.isHidden = false
    }
    
    func releaseWindow() {
        self.lockWindow?.isHidden = true
        self.lockWindow = nil
    }