Now that you are working with state restoration, the application life cycle is going to be a bit different, as you can see in Figure 24.3. Currently, all of your window and rootViewController code is in application:didFinishLaunchingWithOptions:, but with state restoration, it will spread out a bit.
The method application:willFinishLaunchingWithOptions: gets called before state restoration has begun. You should use this method to set up the window and do anything that should happen before state restoration.
In BNRAppDelegate.m, override this method to initialize and set up the window.
- (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; self.window.backgroundColor = [UIColor whiteColor]; return YES; }
Next, update application:didFinishLaunchingWithOptions: to set up the view controller hierarchy in case state restoration did not occur (for example, on the first launch of the application). Also, remove the code that is now in application:willFinishLaunchingWithOptions:.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds]]; // Override point for customization after application launch. // If state restoration did not occur, // set up the view controller hierarchy if (!self.window.rootViewController) { BNRItemsViewController *itemsViewController = [[BNRItemsViewController alloc] init]; // Create an instance of a UINavigationController // Its stack contains only itemsViewController UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:itemsViewController]; // Give the navigation controller a restoration identifier that is // the same name as the class navController.restorationIdentifier = NSStringFromClass([navController class]); // Place navigation controller's view in the window hierarchy self.window.rootViewController = navController; } self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; return YES; }