Domenic Wüthrich Domenic Wüthrich - 5 months ago 57x
Swift Question

swift - present view after entering foreground

I have an app with a login view. In appDelegate I check if the user locks the app with a password. If he does he goes to the login view. (Code called in didFinishLaunchingWithOptions)

let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let nav = storyboard.instantiateViewControllerWithIdentifier(Views.LoginVC.rawValue)
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
self.window?.rootViewController = nav

After logging in I present the actual content in a different navigation controller.

let vc = self.storyboard?.instantiateViewControllerWithIdentifier(Views.MainVC.rawValue)
let rootVC = UINavigationController(rootViewController: vc!)
self.presentViewController(rootVC, animated: false, completion: nil)

When closing the app and entering it again, appDelegate will check again if the user uses a password. If yes, the first code is called again (I checked that, the code gets called)(code called from applicationWillEnterForeground). But instead of going to the login view he stays at the main content view. If he leaves again, an error message pops up in the console.

2016-04-25 20:50:52.714 Point[7222:123809] Snapshotting a view that has not been rendered results in an empty snapshot. Ensure your view has been rendered at least once before snapshotting or snapshot after screen updates.

What do I have to change, so the app presents the login view again when coming back? And how do I avoid this error message?


I just solved my problem by calling the code from applicationDidBecomeActive instead of didFinishLaunchingWithOptions. I don't know why that is so but it works!


No, I think you have an error. You should put your code in applicationWillEnterForeground(), because you want your code to be called once every time the user runs the app.

The app is "inactive" while it's still on the screen, but not active. For example if you double-tap the home button, or if you receive an alert. The method applicationDidBecomeActive() gets called every time the app transitions from inactive state to active.

enter image description here