Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete all keys from a NSUserDefaults dictionary iOS

I use the NSUserDefaults dictionary to store basic information such as high scores etc so that when the user closes the app data is not lost. Anyways I use:

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; 

to store data. If I wish to store a new high score for example then I would do:

[prefs setInteger:1023 forKey:@"highScore"]; [prefs synchronize];  //this is needed in case the app is closed.  

and later if I wish to retrieve the high score I would do:

[prefs integerForKey:@"highScore"]; 

anyways the point is that I store a lot of other things because the NSUserDefaults enable you to store booleans, integers, objects etc. what method would I have to execute to delete all keys so that NSUserDefaults becomes like the fist time I launch the app?

I am looking for something like:

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; [prefs deleteAllKeysAndObjectsInTheDictionary]; 

or maybe there is a way of getting all keys and I have to loop through each object but I don't know how to remove them.

EDIT:

I have tried :

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; [NSUserDefaults resetStandardUserDefaults]; [prefs synchronize]; 

and I still am able to retrieve a high score....

like image 576
Tono Nam Avatar asked Jul 22 '11 23:07

Tono Nam


People also ask

How do you reset NSUserDefaults?

In the simulator top menu: Simulator -> Reset Content and Settings... Show activity on this post. You can use removePersistentDomainForName method available with NSUserDefaults Class.

Which method is used to remove all keys from user default?

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

How do I delete data from UserDefaults?

To remove a key-value pair from the user's defaults database, you need to invoke removeObject(forKey:) on the UserDefaults instance. Let's update the previous example. The output in the console confirms that the removeObject(forKey:) method works as advertised.


2 Answers

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]; } 
like image 172
Alex Nichol Avatar answered Oct 02 '22 23:10

Alex Nichol


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]; 
like image 28
skywinder Avatar answered Oct 02 '22 22:10

skywinder