Fatso
Fatso

Reputation: 1318

Cocoa : Resetting NSUserDefaults

I tried using -resetStandardUserDefaults, I tried removing the plist file, none of those really do what I need. I want to reset my preferences completely, as if the app re-installed. Is there a good solution to this?

I tried :

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

But Xcode complains. Apparently, it doesn't like that the plist file has disappeared. This is Xcode's error : Warning

Upvotes: 1

Views: 1945

Answers (1)

Macmade
Macmade

Reputation: 53950

You can ask NSUserDefaults for all the available keys, and loop in order to removes them:

NSDictionary * allObjects;
NSString     * key;

allObjects = [ [ NSUserDefaults standardUserDefaults ] dictionaryRepresentation ];

for( key in allObjects )
{
    [ [ NSUserDefaults standardUserDefaults ] removeObjectForKey: key ];
}

[ [ NSUserDefaults standardUserDefaults ] synchronize ];

Upvotes: 6

Related Questions