0

Possible Duplicate:
How can I reset the NSUserDefaults data in the iPhone simulator?

In my project i am saving some values in NSUserdefaults with different keys.And i have reset button in my app when i click that button values stored in user defaults should remove.Is there any way to delete those values without keys,and is addSuitedNamed: and removeSuitedName can use in this scenario.

Community
  • 1
  • 1
MobileApp Developer
  • 384
  • 1
  • 3
  • 12

3 Answers3

6
NSUserDefaults * myNSUserDefaults = [NSUserDefaults standardUserDefaults];
NSDictionary * dict = [myNSUserDefaults dictionaryRepresentation];
for (id key in dict) {

     //heck the keys if u need
     [myNSUserDefaults removeObjectForKey:key];
}
[myNSUserDefaults synchronize];

or

[NSUserDefaults resetStandardUserDefaults];
[NSUserDefaults standardUserDefaults];
Shamsudheen TK
  • 30,739
  • 9
  • 69
  • 102
1

You can reset the values of NSUserDefault using resetStandardUserDefaults.

Check this code:

[NSUserDefaults resetStandardUserDefaults];
[NSUserDefaults standardUserDefaults];

Also you can:

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

Or you can use:

NSUSerDefaults *default = [NSUserDefaults standardUserDefaults];
NSDictionary *dictionary = [default dictionaryRepresentation];
for (NSString *key in [dictionary allKeys])
{
    [default removeObjectForKey:key];
}
[default synchronize];
Midhun MP
  • 103,496
  • 31
  • 153
  • 200
1

With a selector being called on a button click you can achieve the same using

- (IBAction)btnResetUserDefaultsPressed:(id)sender {
    NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
    NSDictionary* dictUserDefaults = [userDefaults dictionaryRepresentation];
    for (id akey in dictUserDefaults) {
        [userDefaults removeObjectForKey:akey];
    }
    [userDefaults synchronize];
}

For the second part of your question asked , You would certainly find this useful.

Rahul Sharma
  • 3,013
  • 1
  • 20
  • 47