How to use NSUserDefaults to save state of switch?

Featured Image

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:).

9
Top

Enjoyed this post?

Subscribe to our RSS Feed, Follow us on Twitter and spread it to your friends!

Author

Ivan Kalaica

Ex. Inchooer

At Inchoo, Ivan Kalaica was an iOS SDK developer. He worked with us from 2008. to 2012.

Other posts from this author

Discussion 9 Comments

Add Comment
  1. 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)

  2. Ivan Kalaica

    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.

  3. Mohit

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

  4. Chris

    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

  5. Mashkov Binn

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

  6. hardik

    nice example,
    gr8.

    thanks.

  7. Anupdas

    Thanks for the tutorial, it saved a lot of time…Kudos

  8. Hvala :)

  9. Patty

    Where in my ios code would I put these SAVE and LOAD methods… if I didn’t want to make the user hit SAVE and LOAD buttons each time?

    Just ‘load on launch’ and “save on exit’.

Add Your Comment

Please wrap all source codes with [code][/code] tags.
Top