MatterGoal
MatterGoal

Reputation: 16430

Clearing user defaults programmatically in a OS X application

I'm working at the beta version of my App and i need to be sure that at first launch of the final release users that have been beta tester will start the application with a completely clear user default set... (i can't change the Bundle ID).

1) Is there a way to clear user defaults programmatically ?

2) Will sandboxing this App for the App Store submission helps me ?

Upvotes: 4

Views: 2062

Answers (2)

Joshua Nozzi
Joshua Nozzi

Reputation: 61238

What's wrong with +[NSUserDefaults resetStandardUserDefaults]? If you've registered your defaults properly, this single line will reset to the registered defaults you provided.

Upvotes: 2

Baub
Baub

Reputation: 5054

You can remove it like this:

NSString *appDomain = [[NSBundle mainBundle] bundleIdentifier];
[[NSUserDefaults standardUserDefaults] removePersistentDomainForName:appDomain];

To run it on your first launch, I would just set a BOOL flag. One way to do it is as follows:

- (void)applicationDidFihishLaunching
{
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    if ([[prefs objectForKey:@"secondOrMoreTimeLoading"]boolValue]==0) //so if there is nothing there...
    {
        NSLog(@"This is the first time the user has loaded the application. Welcome!");
        //run the code above here, then change our flag to 1 so that it never runs this again (unless the prefs are reset elsewhere)
        [prefs setObject:[NSNumber numberWithBool:1] forKey:@"secondOrMoreTimeLoading"];
    }
    else{NSLog(@"Welcome back, user.");}     
}

Upvotes: 4

Related Questions