19

I need your help in clarifying my understanding of the various states of an app. I am going to state my interpretation - but please feel free to correct me.

1) App is launched and running in the foreground: state = UIApplicationStateActive
2) User pushes home button: state = UIApplicationStateBackground (????).

 Debug stmt in my app shows it to be Active

3) User double-taps Home and kills the app: state = UIApplicationStateInactive

If the value for case 2 is indeed Active, then when is the value set to Background?

My location-based app relies on this values to take appropriate action for the user.

if ( ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground) ||
    ([UIApplication sharedApplication].applicationState == UIApplicationStateInactive)      ) {
    // isInBackground = YES;
    //  ------ UI not available
}
else {
   // isInBackground = NO;
   //   ------ UI is available
}

Regards, Sam.

Sam
  • 827
  • 4
  • 9
  • 21

3 Answers3

55

When the user taps on the app icon, the app briefly goes through a transitional state of UIApplicationStateInactive on its way to becoming UIApplicationStateActive. This is where the app gets itself ready to display to the user.

When the app is open, the application state is UIApplicationStateActive.

If the user presses the home button, and is returned to the springboard (home screen), or the application is interrupted by something, such as a phone call, the application state transitions back to UIApplicationStateInactive.

For the application state of your app to become UIApplicationStateBackground, your application would have to register for a background process. Look into how to background your location services.

forgot
  • 2,160
  • 2
  • 19
  • 20
  • 3
    @forgot its not completely true.`The app is running in the foreground but is currently not receiving events. (It may be executing other code though.) An app usually stays in this state only briefly as it transitions to a different state.` this said by apple doc . you can know more here.http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html – Kamar Shad Oct 17 '12 at 07:44
  • I understand that, it just didn't seem relavent to the question at the time. I'll edit it to show a more complete answer. – forgot Oct 18 '12 at 02:31
  • 1
    If I lock my screen, with the app in the foreground, I then get UIApplicationStateBackground without applicationDidEnterBackground being called – Bradley Thomas Oct 14 '14 at 14:27
  • 1
    Provided I have UIApplicationExitsOnSuspend set to YES (using iOS 7.1) – Bradley Thomas Oct 14 '14 at 16:16
  • FYI, Swift uses `UIApplicationState.Active` – Dawngerpony Dec 11 '14 at 09:22
5

Apple documentation: UIApplicationState Constants Reference

  • Swift: .Active | Objective-C: UIApplicationStateActive

The app is running in the foreground and currently receiving events.

Available in iOS 4.0 and later.

  • Swift: .Inactive | Objective-C: UIApplicationStateInactive

The app is running in the foreground but is not receiving events. This might happen as a result of an interruption or because the app is transitioning to or from the background.

Available in iOS 4.0 and later.

  • Swift: .Background | Objective-C: UIApplicationStateBackground

The app is running in the background.

Available in iOS 4.0 and later.

Wagner Sales
  • 1,448
  • 2
  • 10
  • 14
1

Some examples:

UIApplicationStateActive - App is running in foreground. Simple.

UIApplicationStateInactive - E.g. App was in the background and is opening through a push notification (transitioning atm). Or the control/notification center is presented above your app. You kind of see it, is in foreground.

UIApplicationStateBackground - App is in the background, but still running. E.g. playing music. Then - this can take a while or not (depending on process you are running in background), but in one point your app is killed. You will see app's snapshot and icon between minimized apps, but the app will be launch again first.

Jakub Truhlář
  • 20,070
  • 9
  • 74
  • 84