Clearing NSUserDefaults

IosObjective CCocoa TouchNsuserdefaults

Ios Problem Overview


I'm using +[NSUserDefaults standardUserDefaults] to store application settings. This consists of roughly a dozen string values. Is it possible to delete these values permanently instead of just setting them to a default value?

Ios Solutions


Solution 1 - Ios

You can remove the application's persistent domain like this:

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

In Swift 3 and later:

if let bundleID = Bundle.main.bundleIdentifier {
    UserDefaults.standard.removePersistentDomain(forName: bundleID)
}

This is similar to the answer by @samvermette but is a little bit cleaner IMO.

Solution 2 - Ios

This code resets the defaults to the registration domain:

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

In other words, it removeObjectForKey for every single key you ever registered in that app.

Credits to Ken Thomases on this Apple Developer Forums thread.

Solution 3 - Ios

Did you try using -removeObjectForKey?

 [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"defunctPreference"];

Solution 4 - Ios

Here is the answer in Swift:

let appDomain = NSBundle.mainBundle().bundleIdentifier!
NSUserDefaults.standardUserDefaults().removePersistentDomainForName(appDomain)

Solution 5 - Ios

If you need it while developing, you can also reset your simulator, deleting all the NSUserDefaults.

iOS Simulator -> Reset Content and Settings...

Bear in mind that it will also delete all the apps and files on simulator.

Solution 6 - Ios

NSDictionary *defaultsDictionary = [[NSUserDefaults standardUserDefaults] dictionaryRepresentation];
for (NSString *key in [defaultsDictionary allKeys]) {
                    [[NSUserDefaults standardUserDefaults] removeObjectForKey:key];
}

Solution 7 - Ios

In Swift:

let defaults = NSUserDefaults.standardUserDefaults()
defaults.dictionaryRepresentation().keys.forEach { defaults.removeObjectForKey($0) }

Solution 8 - Ios

I love extensions when they make the code cleaner:

extension NSUserDefaults {
    func clear() {
        guard let domainName = NSBundle.mainBundle().bundleIdentifier else {
            return
        }

        self.removePersistentDomainForName(domainName)
    }
}

Swift 5

extension UserDefaults {
    func clear() {
        guard let domainName = Bundle.main.bundleIdentifier else {
            return
        }
        removePersistentDomain(forName: domainName)
        synchronize()
    }
}

Solution 9 - Ios

Note: This answer has been updated for Swift too.

What about to have it on one line?

Extending @Christopher Rogers answer – the accepted one.

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

and yes, sometime you may need to synchronize it,

[[NSUserDefaults standardUserDefaults] synchronize];

I've created a method to do this,

- (void) clearDefaults {
    [[NSUserDefaults standardUserDefaults] removePersistentDomainForName:[[NSBundle mainBundle] bundleIdentifier]];
    [[NSUserDefaults standardUserDefaults] synchronize];
}


Swift?

With swift its even more easy.

extension UserDefaults {
    class func clean() {
        guard let aValidIdentifier = Bundle.main.bundleIdentifier else { return }
        standard.removePersistentDomain(forName: aValidIdentifier)
        standard.synchronize()
    }
}

And usage:

UserDefaults.clean()

Solution 10 - Ios

I found this:

osascript -e 'tell application "iOS Simulator" to quit'
xcrun simctl list devices | grep -v '^[-=]' | cut -d "(" -f2 | cut -d ")" -f1 | xargs -I {} xcrun simctl erase "{}"

Source: https://gist.github.com/ZevEisenberg/5a172662cb576872d1ab

Solution 11 - Ios

All above answers are very relevant, but if someone still unable to reset the userdefaults for deleted app, then you can reset the content settings of you simulator, and it will work.enter image description here

Solution 12 - Ios

It's a bug or whatever but the removePersistentDomainForName is not working while clearing all the NSUserDefaults values.

So, better option is that to reset the PersistentDomain and that you can do via following way:

NSUserDefaults.standardUserDefaults().setPersistentDomain(["":""], forName: NSBundle.mainBundle().bundleIdentifier!)

Solution 13 - Ios

Expanding on @folse's answer... I believe a more correct implementation would be...

NSString *appDomain = [[NSBundle mainBundle] bundleIdentifier];
NSDictionary *defaultsDictionary = [[NSUserDefaults standardUserDefaults] persistentDomainForName: appDomain];
    for (NSString *key in [defaultsDictionary allKeys]) {
      NSLog(@"removing user pref for %@", key);
      [[NSUserDefaults standardUserDefaults] removeObjectForKey:key];
    }

...calling NSUserDefault's persistentDomainForName: method. As the docs state, the method "Returns a dictionary containing the keys and values in the specified persistent domain." Calling dictionaryRepresentation: instead, will return a dictionary that will likely include other settings as it applies to a wider scope.

If you need to filter out any of the values that are to be reset, then iterating over the keys is the way to do it. Obviously, if you want to just nuke all of the prefs for the app without regard, then one of the other methods posted above is the most efficient.

Solution 14 - Ios

if the application setting needing reset is nsuserdefault for access to microphone (my case), a simple solution is answer from Anthony McCormick (https://stackoverflow.com/questions/3561321/iphone-how-to-enable-application-access-to-media-on-the-device-alassetslibr).

on the device, go to Settings>General>Reset>Reset Location Warnings

Solution 15 - Ios

Try This, It's working for me .

Single line of code

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTonyNeallonView Question on Stackoverflow
Solution 1 - IosChristopher RogersView Answer on Stackoverflow
Solution 2 - IossamvermetteView Answer on Stackoverflow
Solution 3 - IossboothView Answer on Stackoverflow
Solution 4 - IosjuliensaadView Answer on Stackoverflow
Solution 5 - IosRoger SanoliView Answer on Stackoverflow
Solution 6 - IosfolseView Answer on Stackoverflow
Solution 7 - IosAdam WaiteView Answer on Stackoverflow
Solution 8 - IosValentin SherginView Answer on Stackoverflow
Solution 9 - IosHemangView Answer on Stackoverflow
Solution 10 - IosDaniel Gomez RicoView Answer on Stackoverflow
Solution 11 - IosiDevAmitView Answer on Stackoverflow
Solution 12 - IosSohil R. MemonView Answer on Stackoverflow
Solution 13 - IosmarkeisslerView Answer on Stackoverflow
Solution 14 - IostmrView Answer on Stackoverflow
Solution 15 - IosJaywant KhedkarView Answer on Stackoverflow