0

I have an app with multiple views. I would like the app to always display a "start view" when it is opened again, even if the user is on another view when the app is quit.

jscs
  • 63,694
  • 13
  • 151
  • 195
Philip2905
  • 1
  • 1
  • 4

2 Answers2

1

Your UIApplicationDelegate offers a place where you can hook up into to define what happen when the app become active or comes into the foreground.

Have a look at the method:

– applicationDidBecomeActive:

This method is called to let your application know that it moved from the inactive to active state. This can occur because your application was launched by the user or the system. Applications can also return to the active state if the user chooses to ignore an interruption (such as an incoming phone call or SMS message) that sent the application temporarily to the inactive state.

You should use this method to restart any tasks that were paused (or not yet started) while the application was inactive. For example, you could use it to restart timers or throttle up OpenGL ES frame rates. If your application was previously in the background, you could also use it to refresh your application’s user interface.

In this method you can define which view your app displays when it is launched, both when the app is initially launched and when the app comes back from the background state.

An alternative would be to prevent the app from entering the background state, which means that the app would be always launched and go into the initial state you define. You can do that by setting the "Application does not run in background" (UIApplicationExitsOnSuspend) key in your app plist file to "YES".

sergio
  • 68,819
  • 11
  • 102
  • 123
  • I searched around but I can't find the code for switching from the old view to the start view programmatically. I tried [self popToRootViewControllerAnimated:NO]; but didn't work out. If you know the code for it would you please post it here. Thanks. – Philip2905 Jul 08 '12 at 19:25
  • you should send `popToRootViewControllerAnimated` to your navigation controller; try with: `[[self.window.rootViewController navigationController] popToRootViewControllerAnimated:NO];` or `[self.window.rootViewController popToRootViewControllerAnimated:NO];` depending on what your root view controller is. – sergio Jul 08 '12 at 19:40
  • otherwise, you could try the alternative I suggest: your app will always start from its initial view... – sergio Jul 08 '12 at 19:41
1

In applicationWillEnterForeground: save the state of the app in a local file. In applicationDidBecomeActive: load that state of the app.

Hope this helps. Cheers!

George
  • 4,029
  • 1
  • 23
  • 31
  • How do I save the state of an app in a local file. and then load it? Thanks. – Philip2905 Jul 08 '12 at 19:26
  • You can set codes for your screen ( e.g s1,s2,s3,s4,s5) and write that in a file. Also you can save anything else that you need. The easiest way to implement this would be for exemple to have a global variable NSString *currentScreen. When you have viewDidAppear on s3 , you set currentScreen to "s3" . Saving the state of the app is not done by a method in the SDK or something , you have to do it from scratch. – George Jul 08 '12 at 19:31