Delete all keys from a NSUserDefaults dictionary iOs

Question!

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....

By : Tono Nam


Answers

Does this method not do that:

  (void)resetStandardUserDefaults

From the documentation for NSUserDefaults:

resetStandardUserDefaults

Synchronizes any changes made to the shared user defaults object and releases it from memory.

(void)resetStandardUserDefaults

Discussion

A subsequent invocation of standardUserDefaults creates a new shared user defaults object with the standard search list.

Based on this, you can do:

[NSUserDefaults resetStandardUserDefaults];
[NSUserDefaults standardUserDefaults];

and now the defaults should be reset.

By : PengOne


Swift 2.3:

if let bundleIdentifierBinded = NSBundle.mainBundle().bundleIdentifier{
       NSUserDefaults.standardUserDefaults().removePersistentDomainForName(bundleIdentifierBinded)                                
    }


Oneliner in Swift:

NSUserDefaults.standardUserDefaults().removePersistentDomainForName(NSBundle.mainBundle().bundleIdentifier!)


This video can help you solving your question :)
By: admin