1

I am trying to achieve this func. When user press home button the application will go to background (it will still run) and when he open it again the app will not start in the last view but from the start.

How to detect app went to background and reset it to start view. I think it should be something in app delegate Thank you for all the comments but thats just the 1/2 of answer. How to poptorootvc from app delegate?

Pan Mluvčí
  • 1,242
  • 2
  • 21
  • 42
  • You want every time enter background, kill you app? – Tinyfool Mar 07 '16 at 14:35
  • not kill, just press once home button....suspend the app – Pan Mluvčí Mar 07 '16 at 14:36
  • I'm missing a question over here – David Seek Mar 07 '16 at 14:36
  • 1
    Possible duplicate of [Detect when home button is pressed iOS](http://stackoverflow.com/questions/10324596/detect-when-home-button-is-pressed-ios) – JAL Mar 07 '16 at 14:40
  • 2
    Why do you want to do this? It breaks pretty much everything about how apps are supposed to behave when sent to the background. Imagine your user is in the middle of doing something in your app and receives a text message, they tap the notification to read it and then come back to your app... now they have to start all over and do everything again. – Fogmeister Mar 07 '16 at 15:11
  • Can you do it? Sure... Should you do it? No way. – Fogmeister Mar 07 '16 at 15:11
  • One answer......my BOSS told me to :-D – Pan Mluvčí Mar 07 '16 at 15:12
  • Then ask your boss this question... "Why?". Say that it will break your app. :P – Fogmeister Mar 07 '16 at 15:26
  • I followed this objc answer. [answer](http://stackoverflow.com/questions/18734529/application-will-enter-background-go-to-root-view-menu) solved :-) thanks – Pan Mluvčí Mar 07 '16 at 15:42

2 Answers2

2

I think this is what you're looking for

func applicationDidBecomeActive(application: UIApplication) {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

Put this in your AppDelegate and just switch to whatever view you want in this function.

Alternatively, you could use one of these other AppDelegate functions that detects change of app state

 func applicationWillResignActive(application: UIApplication) {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
}

 func applicationDidEnterBackground(application: UIApplication) {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.

    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
LulzCow
  • 1,199
  • 2
  • 9
  • 21
2

You can put you code at

func applicationWillEnterForeground(application: UIApplication) {

}
Tinyfool
  • 1,460
  • 2
  • 18
  • 40