Delete all keys from a NSUserDefaults dictionary iOS Delete all keys from a NSUserDefaults dictionary iOS swift swift

Delete all keys from a NSUserDefaults dictionary iOS


If you have a look at the NSUserDefaults documentation you will see a method - (NSDictionary *) dictionaryRepresentation. Using this method on the standard user defaults, you can get a list of all keys in the user defaults. You can then use this to clear the user defaults:

- (void)resetDefaults {    NSUserDefaults * defs = [NSUserDefaults standardUserDefaults];    NSDictionary * dict = [defs dictionaryRepresentation];    for (id key in dict) {        [defs removeObjectForKey:key];    }    [defs synchronize];}


Shortest way to do this with the same results like in Alex Nichol's top answer:

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


One-liner:

[[NSUserDefaults standardUserDefaults] removePersistentDomainForName:NSBundle.mainBundle.bundleIdentifier];