Anupam Das
Anupam Das

Reputation: 51

How to clear NSUserDefaults Data?

When I choose a language from language selection list in my app then it shows that language which I selected previously. If I clear the app from my simulator stack or clear it from xcode then run the project, after that it goes ok, and if I want to change the language again then I faced same problem. My code is given below:

- (IBAction)English:(id)sender {

NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
[userDefault setObject:[NSArray arrayWithObjects:@"en", nil] forKey:@"AppleLanguages"];
[userDefault synchronize];

ChooseItemVC *civc = (ChooseItemVC*)[self.storyboard instantiateViewControllerWithIdentifier:@"ChooseItemVC"];
[self.navigationController pushViewController:civc animated:YES];
}

Another language selection code:

- (IBAction)Arabic:(id)sender {

NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
[userDefault setObject:[NSArray arrayWithObjects:@"ar", nil] forKey:@"AppleLanguages"];
[userDefault synchronize];

ChooseItemVC *civc = (ChooseItemVC*)[self.storyboard instantiateViewControllerWithIdentifier:@"ChooseItemVC"];
[self.navigationController pushViewController:civc animated:YES];

}

Upvotes: 3

Views: 6006

Answers (5)

Sarabjit Singh
Sarabjit Singh

Reputation: 1824

The best way to remove all user default vlaues are as follows : Swift code:

UserDefaults.standard.removePersistentDomain(forName: Bundle.main.bundleIdentifier!)

UserDefaults.standard.synchronize()

Objective C

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

Upvotes: 4

Ram
Ram

Reputation: 868

Use following code to remove all existing data in user defaults, i am posting swift code, convert this to objective-c.

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

Upvotes: 1

Nick
Nick

Reputation: 875

For remove data from NSUserDefaults Try this:

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

Upvotes: 0

Dhiru
Dhiru

Reputation: 3060

To remove you data from NSUserDefaults

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

Upvotes: 0

Kamlesh Shingarakhiya
Kamlesh Shingarakhiya

Reputation: 2777

To remove use this

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

Upvotes: 0

Related Questions