3

On Windows, we can write values into registry to know that

but how can I know if my application is the first time it runs on a mac? I need to perform some initialization task.

Thanks

shader
  • 447
  • 3
  • 13

2 Answers2

7

You are looking for the class NSUserDefaults (see Apple Documentation)

For example:

#define kAlreadyBeenLaunched @"AlreadyBeenLaunched"

    if (! [[NSUserDefaults standardUserDefaults] boolForKey:kAlreadyBeenLaunched]) {
        // This is our very first launch

        // Setting userDefaults for next time
        [[NSUserDefaults standardUserDefaults] setValue:[NSNumber numberWithBool:YES] forKey:kAlreadyBeenLaunched];

        // Do your first time stuff
        //<##>
    }

You will use the same class to save and retrieve user preferences.

This values will be saved in ~/Library/Preferences/<your_bundle_id>.plist. This is useful to know for debugging, by looking at the file, but you should not rely on this implementation detail in your code.

Stan James
  • 2,535
  • 1
  • 28
  • 35
Guillaume
  • 21,685
  • 6
  • 63
  • 95
1

There are tons of other people that already asked this.

I guess this one is the most helpful. iPhone: How do I detect when an app is launched for the first time?

Mention: It's working exactly the same way as on the iOS system.

Community
  • 1
  • 1
lbrndnr
  • 3,361
  • 2
  • 24
  • 34