How to clear all NSUserDefaults values ​​in objective-C?

I use NSUserDefaults a lot of time in my application to store some values, but on the Refresh button I want to clear all stored values. Is there a way to clear all NSUserDefaults values?

+4
source share
2 answers

You can delete all stored value using the code below, see here for more details.

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

Or in the shortest way

[[NSUserDefaults standardUserDefaults] setPersistentDomain:[NSDictionary dictionary] forName:[[NSBundle mainBundle] bundleIdentifier]];
+8
source

You can clear your default settings using the following instructions -

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

, -

- (void) refreshUserDefaults
{
    NSString *appDomain = [[NSBundle mainBundle] bundleIdentifier];
    [[NSUserDefaults standardUserDefaults] removePersistentDomainForName:appDomain];
}
+5

All Articles