2

I have an app where I use NSUserDefaults to determine if it is the first time someone opens the app. If it is the first time, the app displays a tutorial page.

Now, I would like to change this so that if the user moves an ON/OFF switch to "ON", they will not see the tutorial when they start up the app. How do I store the user's selection of an ON/OFF switch in NSUserDefaults?

user1486548
  • 1,201
  • 4
  • 15
  • 22

5 Answers5

7

I did something like this with the following code:

Store it:

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setBool:YES forKey:@"the_key"];
[[NSUserDefaults standardUserDefaults] synchronize]; //Thanks Henri Normak

Retrieve it:

[[NSUserDefaults standardUserDefaults] objectForKey:@"the_key"]
ratbum
  • 1,030
  • 12
  • 29
3

Wrap it in an NSNumber numberWithBool:.

[defaults setValue:[NSNumber numberWithBool:mySwitch.on] forKey:@"mySwitchValue"];

// and get it out
BOOL savedSwitch = [[defaults valueForKey:@"mySwitchValue"] boolValue];
danh
  • 62,181
  • 10
  • 95
  • 136
1

You need two bool keys to be saved in NSUserDefaults to get the what you want. e.g. firstTime is used check first time app launch, showTutorial is used to check/save the switch change

You can set your boolean by using:

[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstTime"];

and read it by using this code:

if([[NSUserDefaults standardUserDefaults] boolForKey:@"showTutorial"] || [[NSUserDefaults standardUserDefaults] boolForKey:@"firstTime"]) {
    [self displayTutorial];
    [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"firstTime"];
} 
else {
    [self displayMainScreen];
}

Link the UISwitch on View from InterfaceBuilder to this action on valuechanged

-(IBAction)userSetOnOff:(id)sender
 {
      UISwitch *switchValue = sender;
      if (switchValue.on){
           [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"showTutorial"];
      }
      else{
           [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"showTutorial"];
      }

 }
Vimal Venugopalan
  • 4,091
  • 3
  • 16
  • 25
1

In one of my apps I check to see if NSUserDefaults has been used and if not set the one standard User Default variable.

-(id) init

{

standardUserDefaults = [NSUserDefaults standardUserDefaults];
NSString *testInitState = [standardUserDefaults stringForKey:kInstallInit];
if (testInitState == nil)
{

    #if(kStateModelDebug)
    NSLog(@" State Model: First time initialized");
    #endif
    [self.standardUserDefaults setObject:@"true"  forKey:kInstallInit];
    [self.standardUserDefaults synchronize];

    // Start the tutorials, init DB, etc


}
else 
{
    #if(kStateModelDebug)
    NSLog(@" State Model has ALREADY been initialized");
    #endif

    // Read other state variables and/or init the startup processes

}
return self;    

}

So far , up to ios 5.0 and the new 6.0, this has been a solution. Looking for comments on this solution.

-1

One way is to simply persist a string with the switch status. If the user prefers the on position you'd store the string @"on". Else store the string @"off".

- (void) toggleUserPreference:(BOOL) preferOn {
    NSString *newPreference = preferOn ? @"on" : @"off";
    [[NSUserDefaults standardUserDefaults] setValue:newPreference
                                             forKey:ONOFF_PREFERENCE_KEY];
    [[NSUserDefaults standardUserDefaults] synchronize];
}
SundayMonday
  • 19,147
  • 29
  • 100
  • 154
  • 1
    Or just use `- setBool:forKey:` to store the and `- boolForKey:` to get the result. – Patrick Sep 03 '12 at 17:18
  • 2
    The most straightforward way is to use [boolForKey:](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/nsuserdefaults_Class/Reference/Reference.html#//apple_ref/occ/instm/NSUserDefaults/boolForKey:) – Adam Sep 03 '12 at 17:19