Objective C: Saving User Preferences

Objective-C / Tutorials

Intro:
In this video learn the easiest way to save data to the device in iOS and later load the data to use in your app. We’ll be saving user preferences in a simple application and also demonstrate using a Segmented Control and Switch/Case statements.

Key Code for Saving User Data:

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setInteger:defaultColorInteger forKey:@"colorPreference"];

Key Code for Loading User Data:

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
int defaultColorInteger = [userDefaults integerForKey:@"colorPreference"];

Key Code for Switch / Case Statements:

switch (self.colorControl.selectedSegmentIndex) {
case 0:
[colorView setBackgroundColor:[UIColor redColor]];
break;
case 1:
[colorView setBackgroundColor:[UIColor yellowColor]];
break;
case 2:
[colorView setBackgroundColor:[UIColor greenColor]];
break;
default:
break;
}

Thanks to Jeremy Wyld at QuakeLabs for the tip that [[NSUserDefaults standardDefaults] synchronize]; in this case (and in most cases) is not necessary and would create unnecessary overhead.
Julian Bryant


Related Posts

iOS App Starts Trounce Android Development

iOS App Starts Trounce Android Development

Building Your First App: The “HelloWorld” Tour of XCode

Building Your First App: The “HelloWorld” Tour of XCode

Image of Pop-up Window for Setting Properties

Quick Tip: Shortcut for setting @property/@synthesize in Xcode

Mobile Developers Excited About iCloud & Google+

Nav Bar 3: Using Custom Images in the Nav Bar for iOS

Nav Bar 3: Using Custom Images in the Nav Bar for iOS

Nav Bar 2: Customizing the Navigation Bar in iOS

Nav Bar 2: Customizing the Navigation Bar in iOS