Pete Brennan
Pete Brennan

Reputation: 1

Detect if iOS application was resumed with application switcher or from homescreen?

I need to detect if a user resumes my application from the homescreen or from the application switcher. This is very easy on Android, but on iOS I'm experiencing the same behavior.

In both cases both delegate methods get called

applicationWillEnterForeground and applicationDidBecomeActive

as you would expect.

In case of answers like "There shouldn't be a difference in behavior": my app should display a menu when opened from the homescreen (opened "fresh"), but should resume when switched back to.

Edit:

For clarification I want to make a tap on the homescreen icon seem like the app started fresh even though it might have been suspended.

Upvotes: 0

Views: 1436

Answers (1)

SwiftArchitect
SwiftArchitect

Reputation: 48524

I believe that what you are really trying to detect:

  1. is it a fist launch -vs.-
  2. is my app already running.

There is an inherent problem with this approach: unlike Android, where sessions are fuzzy and apps are generally running in the background, iOS hides the state of all applications to the user : once launched, it is just about permanently present in the fast-app-switching list. Until, that is, the user explicitly removes the app from that list (*).

(*) Even so, the consequences on your app vary vastly ; if your app was running, then it quits. But your app may already have been stopped long ago, in case the iOS needed to reclaim the memory for another frontmost application. Right there, you are bound to get inconsistencies.

(**) Furthermore, the very same happens from the Springboard. As you know, the app may already be running.

You can only differentiate fresh launch from hot swap using:

  // Fresh launch
  - (BOOL)application:(UIApplication *)application
        didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

vs.

  // App already running
  - (void)applicationWillEnterForeground:(UIApplication *)application

There is more.

You have some granularity on how exactly you were launched (as in, say, from a URL or another app). See UIApplicationLaunchOptionsURLKey and all the other provided with -application:didFinishLaunchingWithOptions:.

Finally, many apps just display a splash screen each and every time they are brought to the forground. Since working around the iOS is generally brittle and bound to break at the next update, you may just want to do that. Less code, more robustness, consistency all over.

Upvotes: 1

Related Questions