0

I need to store a boolean value that would persist only for the app's running time. When the user quit the app (from the background as well) that variable should reset to default. How can I do this?

I tried constants. I keep constants in a separate .h file. In it I declared it like this.

const BOOL hasShownTutorial = NO;

And in the view controller,

if (hasShownTutorial == NO) {
    [self showAppTutorial];
    hasShownTutorial = YES;
}

I get an error at hasShownTutorial = YES; saying Read-only variable is not assignable.

I also tried going about this using NSUserDefaults. But the thing is the value is stored for good once you set it. Is there a way to clear it up when the app quits?

I'd appreciate your input and suggestions.

Thank you.

Isuru
  • 30,617
  • 60
  • 187
  • 303

2 Answers2

1

Store it in NSUserDefaults under a specific key.

[[NSUserDefaults standardDefaults] setBool:<myBool> forKey:@"myKey"];

Then to retrieve it later.

BOOL b = [[NSUserDefaults standardDefaults] boolForKey:@"myKey"];

If you want to clear the data.

NSString *domain = [[NSBundle mainBundle] bundleIdentifier];
[[NSUserDefaults standardUserDefaults] removePersistentDomainForName:domain];

For example, in your AppDelegate's implementation of -applicationWillTerminate:, just clear the data.

- (void)applicationWillTerminate:(UIApplication *)application
{
    NSString *domain = [[NSBundle mainBundle] bundleIdentifier];
    [[NSUserDefaults standardUserDefaults] removePersistentDomainForName:domain];
}

Edit: If the first method of clearing the data does not work, you can use the class method +resetStandardUserDefaults. For example the following would clear the current defaults.

[NSUserDefaults resetStandardUserDefaults];

A third possiblity is to specifically remove a property.

[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"myKey"];
Brian Tracy
  • 6,801
  • 2
  • 33
  • 48
  • Is applicationWillTerminate called if I kill the application (2x Home, swipe up)? – Lord Zsolt May 30 '14 at 06:49
  • I tried this and it works :) However I have one question. Is it possible to remove specific values within `applicationWillTerminate` like [this](http://stackoverflow.com/a/545099/1077789)? – Isuru May 30 '14 at 06:50
  • 1
    @LordZsolt yes. It is called right before the application quits. For 2x home, swipe up, this might be into the background state, and there is another method for that in the app delegate. – Brian Tracy May 30 '14 at 06:50
  • 1
    @Isuru If you remove it like that, at next launch the value will be nil. Didn't you want to reset it to a default value? (Though for that you should user setValue:defaultValue forKey:key – Lord Zsolt May 30 '14 at 06:52
  • @LordZsolt Yes, I just needed to know. I'm not doing that to this app I'm working on. Thanks :) – Isuru May 30 '14 at 06:54
0

You can't change const value since it is a read only memory. Use static keyword instead.

ujell
  • 2,792
  • 3
  • 17
  • 23
  • I forgot to mention, I did that too. But didn't accomplish what I wanted. – Isuru May 30 '14 at 06:36
  • The value reset itself when I loaded the view controller again. It's okay. I found a solution that works with `NSUserDefaults` :) – Isuru May 30 '14 at 06:52
  • You probably changed it in `viewDidLoad`. Also, it is not good practise to use persistent storage (like `NSUserDefaults`), if you don't need to store data. – ujell May 30 '14 at 07:13