In didFinishLaunchingWithOptions
, how do you know if the app is resuming from the foreground or is a new launching? I need to know in this method because there are 2 tasks to run depending on what the launchOptions
are and what state the app is in. Thank you.

- 4,403
- 8
- 32
- 43
2 Answers
From Test if app did become active from a UILocalNotification:
When an app enters the foreground from the background it does not trigger
applicationDidFinishLaunchingWithOptions
. It does, however, callapplicationWillEnterForeground
andapplicationDidBecomeActive
. This can be verified with a couple of NSLogs.
So it's possible to know if the app returns from background or if it's a new launch.
From the developer page: search for didFinishLaunchingWithOptions
and you will have all the options. Also, they have this diagram telling all the stages that the app has:
What you are looking is for the Inactive State:
The app is running in the foreground but is 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. Upon entering this state, the app should put itself into a quiescent state with the expectation of moving to the background or active state shortly.
Or the Suspended State:
The app is in memory but is not executing code. The system suspends apps that are in the background and do not have any pending tasks to complete. The system may purge suspended apps at any time without waking them up to make room for other apps.
After reading this I don't think that what you need is Background State

- 1
- 1

- 642
- 2
- 13
- 34
-
Interesting...I'm using branch.io to set up deep linking for the app and can see print statements in didFinishLaunchingWithOptions from resuming to foreground. – jamesfzhang Jan 11 '16 at 18:31
-
but can you get the information that you need with didFinishLaunchingWithOptions? – Ricardo Alves Jan 11 '16 at 18:39
-
Yes I did, didFinishLaunchingWithOptions has data from the deep link when the app first launches as well as when it enters foreground from background. – jamesfzhang Jan 11 '16 at 18:54
-
Yes, I resolved my problem by setting a boolean value in `applicationDidEnterBackground` to determine entering foreground from background vs. fresh app launch. – jamesfzhang Jan 11 '16 at 20:19
-
Nicely done. I will try to remember that if I need it one day – Ricardo Alves Jan 11 '16 at 20:20
-
Update your question with the answer to help other than – Ricardo Alves Jan 11 '16 at 20:21
didFinishLaunchingWithOptions
method calls only with a new launch. It can be regular launch in foreground, or it can be app relaunching in background when you use Background Modes
. To define the details of launching see the launchOptions
.
When app goes into background or foreground see applicationDidEnterBackground
and applicationWillEnterForeground
methods.
Here is how Branch defines launch options (https://github.com/BranchMetrics/iOS-Deferred-Deep-Linking-SDK/blob/master/Branch-SDK/Branch-SDK/Branch.m) :
- (void)initSessionWithLaunchOptions:(NSDictionary *)options isReferrable:(BOOL)isReferrable explicitlyRequestedReferrable:(BOOL)explicitlyRequestedReferrable automaticallyDisplayController:(BOOL)automaticallyDisplayController {
self.shouldAutomaticallyDeepLink = automaticallyDisplayController;
self.preferenceHelper.isReferrable = isReferrable;
self.preferenceHelper.explicitlyRequestedReferrable = explicitlyRequestedReferrable;
if ([BNCSystemObserver getOSVersion].integerValue >= 8) {
if (![options objectForKey:UIApplicationLaunchOptionsURLKey] && ![options objectForKey:UIApplicationLaunchOptionsUserActivityDictionaryKey]) {
[self initUserSessionAndCallCallback:YES];
}
else if ([options objectForKey:UIApplicationLaunchOptionsUserActivityDictionaryKey]) {
self.preferenceHelper.isContinuingUserActivity = YES;
}
}
else {
if (![options objectForKey:UIApplicationLaunchOptionsURLKey]) {
[self initUserSessionAndCallCallback:YES];
}
}
}

- 1,913
- 15
- 21