Detecting the first ever run of an app
Asked Answered
O

3

5

I am creating an app in which I have to create a plist when the app launches for the first time. I'm later going to use the plist to store details a user later inputs. How can I detect the first launch of the app? I was experimenting with NSUserDefaults but I think I'm doing something wrong.

Ogbomosho answered 13/10, 2011 at 18:33 Comment(0)
W
15

You can do this with NSUserDefaults. But be careful with the Version Number.

Do the following:

NSString *bundleVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:(NSString *)kCFBundleVersionKey];

NSString *appFirstStartOfVersionKey = [NSString stringWithFormat:@"first_start_%@", bundleVersion];

NSNumber *alreadyStartedOnVersion = [[NSUserDefaults standardUserDefaults] objectForKey:appFirstStartOfVersionKey];
if(!alreadyStartedOnVersion || [alreadyStartedOnVersion boolValue] == NO) {
    [self firstStartCode];
    [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithBool:YES] forKey:appFirstStartOfVersionKey];
}

the selector firstStartCode will only be called on time for each application version on the very first run.

Okay?

Wolfe answered 13/10, 2011 at 18:39 Comment(0)
B
2

I like to use NSUserDefaults to store an indication of the the first run.

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];  
if (![defaults objectForKey:@"firstRun"])
  [defaults setObject:[NSDate date] forKey:@"firstRun"];

[[NSUserDefaults standardUserDefaults] synchronize];

You can then test for it later...

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];        
if([defaults objectForKey:@"firstRun"])           
{
  // do something or not...
}

Taken from: Best way to check if an iPhone app is running for the first time

Bolus answered 13/10, 2011 at 18:34 Comment(1)
so the first block of code, do i put it in the app delegate under didFinishLaunching?Ogbomosho
N
2

You can use the following:

-(void) firstLaunch {
    //Code goes here
}

-(void) firstLaunchCheck {
    if(![[NSUserDefaults standardUserDefaults] boolForKey:@"didLaunchFirstTime"]) {
        [[NSUserDefaults standardUserDefaults] setBool:TRUE forKey:@"didLaunchFirstTime"];
        [self firstLaunch];
    }
}
Natishanative answered 14/10, 2011 at 2:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.