SNos SNos - 1 year ago 58
iOS Question

Return to main view controller when app goes in background

In my app I have tree view controllers, when the app enters background I want the app to get back to the main view controller. My app is a single application without


I know that I have to implement this inside:

func applicationWillResignActive(application: UIApplication) {


but I am not sure how to tell the app to return to the main view controller.

How can I do this?


I have made a small example that takes the app to the main view controllers when enters background.
The first Time it goes background the app works fine after I start to get a warning. How can I resolve the warning?

Answer Source

Here is your fixed project. The catch was to just to set the rootViewcontroller of Appdelegate's window like this when the application goes to background

 let mainStoryBoard = UIStoryboard(name: "Main", bundle: nil)
 let mainViewController = mainStoryBoard.instantiateViewControllerWithIdentifier("MainView") as! ViewController
 let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
 appDelegate.window?.rootViewController = mainViewController