atomikpanda
atomikpanda

Reputation: 1886

Check for first launch of my application

How would I check if it is the first launch of of my application using NSUserDefaults and running some code for the first time my app opens?

Upvotes: 6

Views: 6298

Answers (4)

Omar
Omar

Reputation: 105

if (![[NSUserDefaults standardUserDefaults] boolForKey:@"hasBeenLaunched"]) {
    // Run code on the first launch only ...
    [defaults setBool:YES forKey:@"hasBeenLaunched"];
}

You can use NSUserDefaults to save bools, integers, objects into the program and have them available whenever you open it. You can use 'boolForKey' to set a flag called "hasBeenLaunched". By default, this value will be NO when not set. Once you change it to YES, the code in the if condition will never be executed again.

Upvotes: 2

NSGod
NSGod

Reputation: 22948

In your main controller class, implement something like this:

static NSString * const MDFirstRunKey  = @"MDFirstRun";


@implementation MDAppController

+ (void)initialize {
   NSMutableDictionary *defaults = [NSMutableDictionary dictionary];
   [defaults setObject:[NSNumber numberWithBool:YES] forKey:MDFirstRunKey];
   [[NSUserDefaults standardUserDefaults] registerDefaults:defaults];
   // the following if on Mac and is necessary:
   [[NSUserDefaultsController sharedUserDefaultsController] setInitialValues:defaults];
}

- (void)applicationDidFinishLaunching:(NSNotification *)notification {

  BOOL firstRun = [[[NSUserDefaults standardUserDefaults]
                      objectForKey:MDFirstRunKey] boolValue];

  if (firstRun) {
     // do something

     [[NSUserDefaults standardUserDefaults] setObject:
                [NSNumber numberWithBool:NO] forKey:MDFirstRunKey];

  } else {
     // do something else

  }
}

@end

The +initialize class method is called before an instance of the class it's found in is created; in other words, it is called very early on, and is a good place to set up your default values.

See Preferences and Settings Programming Guide: Registering Your App's Default Preferences for more info.

Upvotes: 1

Chris Trahey
Chris Trahey

Reputation: 18290

The NSUserDefaults answer is the first thing that popped in my head, but upon reflection I will make another suggestion. A bit more work, but it's worth considering. The motive is: sometimes when troubleshooting an app, Apple recommends deleting that app's plist file. It's a fairly ubiquitous troubleshooting technique. I would recommend storing your boolean in your plist file instead of NSUserDefaults.

Disclaimer: I only do iOS development, so I'm not sure how NSUserDefaults and plists interact on the Mac, and I don't know what all is involved in getting your plist to live in ~/Library/Application\ Support/Preferences/com.mycompany.MyAppName.plist

Anyway, I imagine what this requires is having some code which can actually author a "fresh" plist (probably a copy from a template file in your bundle), and you app does this if it launches and does not see a plist. The default plist should not include the flag which lets your users skip the 'first time' code, but if they have opened the app before, and then delete the plist, they should get default behavior back.

This is an important behavior to support where possible, to aide our users if our app ever gives them trouble.

Upvotes: 2

Matt Wilding
Matt Wilding

Reputation: 20153

This should point you in the right direction:

static NSString* const hasRunAppOnceKey = @"hasRunAppOnceKey";
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
if ([defaults boolForKey:hasRunAppOnceKey] == NO)
{
    // Some code you want to run on first use...
    [defaults setBool:YES forKey:hasRunAppOnceKey];
}

Upvotes: 20

Related Questions