I'm writing an OS X app. I'm trying to clear NSUserDefaults.
I'm currently doing:
[[NSUserDefaults standardUserDefaults] removePersistentDomainForName:[[NSBundle mainBundle] bundleIdentifier]];
But it's not working, i.e. all the key-value pairs are still there when I print the user defaults.
I've also tried adding:
[[NSUserDefaults standardUserDefaults] synchronize];
still no luck.
Can anyone please help? Thanks in advance!
It seems that removePersistentDomainForName: does not work on OS X 10.8. Furthermore, these things are not functional also:
[[NSUserDefaults standardUserDefaults] setPersistentDomain:@{} forName:[NSBundle mainBundle].bundleIdentifier];
[[NSUserDefaults standardUserDefaults] setValuesForKeysWithDictionary:@{}];
To work around the problem, you may set an arbitrary key-value pair to the domain then remove the pair:
[[NSUserDefaults standardUserDefaults] setPersistentDomain:@{@"": @""} forName:[NSBundle mainBundle].bundleIdentifier];
[[NSUserDefaults standardUserDefaults] removeObjectForKey:@""];
NSLog(@"%@", [[NSUserDefaults standardUserDefaults] persistentDomainForName:[NSBundle mainBundle].bundleIdentifier]); // => (null)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With