Quantcast
Channel: Developer Feed - Snippet
Viewing all articles
Browse latest Browse all 178

Using NSUserDefaults to store Settings in iOS

$
0
0

iOS NSUserDefaultsNSUserDefaults is one of simplest ways to store your configuration/settings of your iPhone/iPad application. You don't have to create a plist or work with the file system, it encapsulates all of that.

The value that is to be stored can be instance of  NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary. Internally this API knows how to serialize/deserialize the instance.

<!--break-->

  1. // get the handle
  2. NSUserDefaults*defaults =[NSUserDefaults standardUserDefaults];
  3.  
  4. NSString*key =@"username";
  5. NSString*value =@"john";
  6.  
  7. // set the value
  8. [defaults setObject:value forKey:key];
  9.  
  10. // save it
  11. [defaults synchronize];
  12.  
  13. // Get the result
  14. NSString*retrievedValue =[defaults stringForKey:key];

Viewing all articles
Browse latest Browse all 178

Trending Articles