Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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];

}
like image 534
Anupam Das Avatar asked Jun 24 '26 15:06

Anupam Das


1 Answers

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];
like image 147
Sarabjit Singh Avatar answered Jun 27 '26 04:06

Sarabjit Singh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!