iOS: Use a boolean in NSUserDefaults

60,362

Solution 1

You can set your boolean by using:

[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"logged_in"];
[[NSUserDefaults standardUserDefaults] synchronize];

and read it by using this code:

if(![[NSUserDefaults standardUserDefaults] boolForKey:@"logged_in"]) {
    [self displayLogin];
} else {
    [self displayMainScreen];
}

Solution 2

There is a method in NSUserDefaults called registerDefaults:. You use this method to set your application's "default defaults." Basically, you create an NSDictionary containing your default keys and values (in your case a NO for a "saved credentials" key), and you register it using registerDefaults:. This if often done in app delegate's + (void)initialize method to ensure that your defaults are registered before they are needed. These values only get used if your app hasn't replaced them. In other words, they won't be used unless the key you're looking for isn't in the Application Domain, i.e., the user defaults read from the user's .plist file.

On the other hand, you could just check for login credentials and pop up an alert if they're missing. This eliminates the need to keep your boolean value synchronized with the login credentials. If you later provide a "delete login credentials" capability, you won't have to remember to set the boolean back to NO. If your login credentials are saved in user's defaults, you'd do this:

NSString *userID = [[NSUserDefaults standardUserDefaults] stringForKey:@"userID"];
NSString *password = [[NSUserDefaults standardUserDefaults] stringForKey:@"password"];
if (userID != nil && password != nil) {
    // Code to log user in
} else {
    // Code to pop up an alert
}

Solution 3

This solution, suggested by Henrik P. Hessel:

if(![[NSUserDefaults standardUserDefaults] boolForKey:@"logged_in"]) {
[self displayLogin];
} else {
[self displayMainScreen];
}

would work in your case, but bear in mind that you shouldn't check with this code if some key is set at all, because if it actually is set and set to "NO", you would still get a result as if was not set at all. Instead I would use

if([[NSUserDefaults standardUserDefaults] objectForKey:@"logged_in"] == nil) {
    //Do something
}

Solution 4

You don't need to first set it to NO, instead you may check if a key has been set at all. If not, and if your app determines the credentials are complete, just create it and set it to YES.

Check my answer to another question to see how I usually do this.

Share:
60,362
Sebastien Peek
Author by

Sebastien Peek

After being exposed to programming from a young age, I have continued to grow my knowledge in multiple programming languages, especially in Objective-C and other C based languages. The last few years have seen me involved in many different projects. I have been playing a lot with services in my own time, specifically with Node.js.

Updated on June 18, 2021

Comments

  • Sebastien Peek
    Sebastien Peek almost 3 years

    When the rootViewController of my application is loaded, I want to be able to check whether or not the users login credentials have been saved to NSUserDefaults.

    Basically, when the user loads the application and he/she doesn't have her login credentials saved, a modalAlertView will be pushed and the user will be able to save their credentials appropriately. This saves those UITextField strings to a respective NSUserDefault object. However, is it possible, that when this saving is done, I can create an NSUserDefault object which is a boolean and change the value to a yes?

    Meaning that the boolean is already set to no, and when the user saves their login credentials, it also changes the boolean to a yes?

  • Sebastien Peek
    Sebastien Peek over 13 years
    how would I go about writing a method that checks the login credentials and just pops up the alert if they're not saved?
  • Ajay
    Ajay almost 13 years
    @Henril P .Can it display the same user which was login previously
  • Krzysztof Przygoda
    Krzysztof Przygoda about 10 years
    You shouldn't even think about storing passwords in NSUserDefaults.