1

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.

jamesfzhang
  • 4,403
  • 8
  • 32
  • 43

2 Answers2

2

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, call applicationWillEnterForeground and applicationDidBecomeActive. 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:

State changes in an iOS app

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

Community
  • 1
  • 1
Ricardo Alves
  • 642
  • 2
  • 13
  • 34
0

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];
        }
    }
}
shpasta
  • 1,913
  • 15
  • 21