Objective-C Language

NSUserDefaults

Simple example

For example:

FOR SAVING:

  NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

  // saving an NSString
  [prefs setObject:txtUsername.text forKey:@"userName"];
  [prefs setObject:txtPassword.text forKey:@"password"];

  [prefs synchronize];

FOR RETRIEVING

  NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

  // getting an NSString
 NSString *savedUsername = [prefs stringForKey:@"userName"];
 NSString *savedPassword = [prefs stringForKey:@"password"];

Clear NSUserDefaults

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

This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow