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

1
  • ~/Library/Application Data perhaps?
    – You
    Dec 29, 2011 at 15:17

2 Answers 2

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.

0
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.

1
  • It's working exactly the same way. In case you voted my answer down, try it out.
    – lbrndnr
    Dec 29, 2011 at 15:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.