1

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

Fatso
  • 1,278
  • 16
  • 46
  • possible duplicate of [Clearing NSUserDefaults](http://stackoverflow.com/questions/545091/clearing-nsuserdefaults) – sbooth Nov 24 '11 at 16:48

1 Answers1

6

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 ];
Macmade
  • 52,708
  • 13
  • 106
  • 123
  • Thanks for that : just one question : Xcode moans when I run this code, because it says the plist file has disappeared. What can I do about this? – Fatso Nov 24 '11 at 17:46
  • Why do you have the plist in XCode? The warning is normal in such a case. – Macmade Nov 24 '11 at 17:53
  • What about stuff like the "AppleLanguages"? I don't know if you'll run into trouble deleting it or other "system provided entries" – Zaky German Nov 24 '11 at 17:56
  • That's the problem, Macmade : the plist isn't in Xcode. Xcode just complains. – Fatso Nov 24 '11 at 18:30
  • If it complains, it means you are editing it with XCode. It may not be in your project. – Macmade Nov 24 '11 at 18:32
  • Macmade : I was able to solve the problem by not calling synchronize. That makes sense, of course. I don't know why Xcode complained though. – Fatso Nov 24 '11 at 18:52
  • This way is slow in my experience – barfoon Jan 27 '12 at 17:53