Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to determine in applicationDidBecomeActive whether it is the initial iPhone app launch?

how to determine in how to determine in UIApplicationDidBecomeActiveNotification whether it is the initial app launch?whether it is the initial app launch?

that is the initial start up of the application, as opposed to subsequent DidBecomeActive's due to the application being put in background and then to foreground (e.g. user goes to calendar then back to your app)

like image 814
Greg Avatar asked Oct 11 '11 23:10

Greg


1 Answers

FWIW, the accepted answer tells you if the app has ever been launched before, not if the app is resuming from the background vs launching. Once the alreadyLaunched key has been set in preferences it will return YES when the app is launched in the future (vs resumed from background).

To detect if the app has resumed from the background you don't need to add anything to preferences. Rather, do the following in your app delegate implementation.

// myAppDelegate.m
//

@interface MyAppDelegate()
@property (nonatomic) BOOL activatedFromBackground;
@end

@implementation MyAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.activatedFromBackground = NO;

    // your code
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    self.activatedFromBackground = YES;

    // your code
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    if (self.activatedFromBackground) {
        // whatever you want here
    }
}

@end
like image 188
XJones Avatar answered Sep 20 '22 00:09

XJones