2

i want to save a bool in my application which can be modified. At the first time it is "NO" and when a user enter a right code in the application the bool must be true. And when the user starts the app again the bool still has to be true. I think i have to declare the bool in the appDelegate but i´m not sure. Or is there a better way? Does anyone know to solve this problem?

  • possible duplicate of [How to use a boolean in NSUserDefaults](http://stackoverflow.com/questions/3841166/how-to-use-a-boolean-in-nsuserdefaults) – Rafał Sroka Jul 31 '14 at 12:29

3 Answers3

1

Add a method to save the bool to NSUserDefaults:

- (void)saveTheBool:(BOOL)b
{
    NSUserDefaults *userDefaults = [NSUserDefaults sharedUserDefaults];
    [userDefaults setBool:b forKey:@"TheKeyForMyBool"];
    [userDefaults synchronize];
}

and then reload the bool using:

- (BOOL)loadTheBool
{
    NSUserDefaults *userDefaults = [NSUserDefaults sharedUserDefaults];
    return [userDefaults boolForKey:@"TheKeyForMyBool"];
}

You could add these methods to your App Delegate, or any other suitable class. If you use App Delegate then you can call [self loadTheBool] within the application:didFinishLaunchingWithOptions: method.

Droppy
  • 9,691
  • 1
  • 20
  • 27
0

You can persist state with NSUserDefaults

azsromej
  • 1,619
  • 13
  • 15
0

You can use the Localstorage concept for storing the value of variable even when application is closed once. By setting and getting value of variables in local storage, you can persist the value of variable in your application.

You can search about the implementation of local storage in objective-c on google.

Vaishali Modi
  • 654
  • 1
  • 5
  • 17