How to use NSUserDefaults to save state of switch?

5 Comments 24th MAR 2009 | Posted by Ivan Kalaica in iPhone dev

How to use NSUserDefaults to save state of switch?

If you have an application that should save for example state of a switch and you don’t want use database for that small request you can simply add instance of NSUserDefaults class in your implementation. It is easy job, see below;

- (IBAction) saveState {

NSString *value = @"ON";
NSUserDefaults *userPreferences = [NSUserDefaults standardUserDefaults];
if(!switch.on){
value = @"OFF";
[userPreferences setObject:value forKey:@"stateOfSwitch"];
}
[userPreferences setObject:value forKey:@"stateOfSwitch"];

}

This is part of code that loads value in a NSString *value for key stateOfSwitch. When you start your application again and view finisheds with loading button will be in lefted state.

- (void)viewWillAppear:(BOOL)animated {

NSString *_value= [[NSUserDefaults standardUserDefaults] stringForKey:@"stateOfSwitch"];

if([_value compare:@"ON"] == NSOrderedSame){
switch.on = YES;
}
else {
switch.on = NO;
}
[super viewWillAppear:animated];
}

NSUserDefaults class gives us an opportunity to save values without database use. This class uses KVC structure model for saving and loading values. Key-value coding (KVC) defines generic property accessor methods—valueForKey: and setValue:forKey:—which identify properties with string-based keys.

KVC is not meant as a general alternative to using accessor methods—it is for use by code that has no other option, because the code cannot know the names of the relevant properties at compile time. Key-value coding and the dot syntax are orthogonal technologies. You can use KVC whether or not you use the dot syntax, and you can use the dot syntax whether or not you use KVC. Both, though, make use of a “dot syntax”.

In the case of KVC, the syntax is used to delimit elements in a key path. It is important to remember that when you access a property using the dot syntax, you invoke the receiver ’s standard accessor methods (as a corollary, to emphasize, the dot syntax does not result in invocation of KVC methods valueForKey: or setValue:forKey:).

If you like what you read, please share it.

  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Yahoo! Bookmarks
  • Reddit
  • Technorati
  • Twitter
  • StumbleUpon
  • LinkedIn
  • Netvibes
  • NewsVine
  • Sphinn
  • Tumblr
  • Posterous

To post code in comments, place your code inside [code] and [/code] tags.

There are 5 comments (Add Yours +)

  • I actually always just use setBool:forKey: to set the values of switches, since they are already BOOL’s and it just seems unintuitive to have to slap NSStrings into the mix.

    Also should be noted that there are other useful methods in NSUserDefaults:

    – setBool:forKey:
    – setFloat:forKey:
    – setInteger:forKey:
    – setObject:forKey:

    All quite self explanatory (see more here: http://developer.apple.com/DOCUMENTATION/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/Reference/Reference.html)

  • Ivan Kalaica Says

    Thanks for advice Sahil Desai! I have by mistake put “YES” and “NO” – I ment “ON” and “OFF”. That’s why I have decided to use a string. Just to show how to save state.

  • Is there is advisable to use NSUserDefaults for an application to save UserName, Email instead of Sqlite

  • thanks for the info. I was curious, do you know how to save the state of a UIButton? I have an app where a UIButton is disabled when a user taps it. I just need to find a way to save the state of the button for the next time the user launches the application. Any help would be great.
    -Thank you

  • Mashkov Binn Says

    Or this::

    static const NSString *kSwitchStateKey = @”stateOfSwitch”;

    - (IBAction) saveState {
    [NSUserDefaults standardUSerDefaults] setBool:switch.on forKey:kSwitchStateKey];
    }

    - (void)viewWillAppear:(BOOL)animated {
    switch.on = [NSUserDefaults standardUSerDefaults] boolForKey:kSwitchStateKey];
    }

Leave a Comment

Please wrap all source codes with [code][/code] tags.
Magento Design and Development | Magento SEO | iPhone Application Development Web Application Development with ZEND | WordPress Ecommerce | WordPress development
Sitemap

Inchoo - webappsolutions | 2009