Search code examples
ioscocoa-touchnsuserdefaultsuiswitch

Store ON/OFF switch in NSUserDefaults?


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?


Solution

  • 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"]