IOS: 在 NSUserDefault 中使用布尔值

当加载应用程序的 rootViewController时,我希望能够检查用户登录凭据是否已保存到 NSUserDefaults

基本上,当用户加载应用程序时,他/她没有保存她的登录凭据,就会推送一个 modalAlertView,用户就能够适当地保存他们的凭据。这会将这些 UITextField字符串保存到相应的 NSUserDefault对象中。但是,有没有可能,当这个保存完成后,我可以创建一个 NSUserDefault对象,它是一个布尔值,并将值更改为 yes?

这意味着布尔值已经被设置为 no,当用户保存他们的登录凭据时,它也将布尔值更改为 yes?

61272 次浏览

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

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.

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
}

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
}

Swift 2.3

To store values

    NSUserDefaults.standardUserDefaults().setBool(false, forKey: "logged_in")

For fetching values

  if NSUserDefaults.standardUserDefaults().boolForKey("logged_in") == true {
dashboardScreen()
}else {
loginScreen()
}

Swift 3.0

To store values

  UserDefaults.standard().setBool(true, forKey: "logged_in")

to retrieve values

  UserDefaults.standard().bool(forKey: "logged_in"){
dashboardScreen()
}else {
loginScreen()
}

in Swift 3

let Defaults = UserDefaults.standard()()
let exists = Defaults.bool(forKey: "isLoggedIn")


if !exists {
displayLoginScreen()
} else {
print("value exists..do something")
displayMainScreen()
}

alternatively we can use object(forKey:) then cast it to bool

if let exists = Defaults.object(forKey: "isLoggedIn"), let isLoggedIn = exists.boolValue where isLoggedIn == true {
displayMainScreen()
} else {
displayLoginScreen()
}