Thursday, April 19, 2012

Clearing NSUserDefaults

I needed to remove all the settings from an app, and there's not an easy built in way to do it.  While it seems like it might, this does not do it:
[NSUserDefaults resetStandardUserDefaults];
But this does:

NSDictionary *settings = [[NSUserDefaults standardUserDefaults] dictionaryRepresentation];
NSArray *keys = [settings allKeys];
for (int i=0; i<[keys count]; i++) {
   NSString *key = [keys objectAtIndex:i];
   [[NSUserDefaults standardUserDefaults] removeObjectForKey:key];
[[NSUserDefaults standardUserDefaults] synchronize];

No comments: