0

I've searched on google and on here, no luck so far.

To find out whether my application has been launched before, I use code similar to the code in this StackOverflow answer.

The main difference is the negation of the key (FirstLaunch instead of HasLaunched) :

configuration = [NSUserDefaults standardUserDefaults];
[configuration registerDefaults:[[NSDictionary alloc] initWithObjectsAndKeys:@"YES", @"FirstLaunch", nil]];
if ([configuration boolForKey:@"FirstLaunch"]) {
    NSLog(@"aha");
    // aha
    [configuration setBool:NO forKey:@"FirstLaunch"];
    [configuration synchronize];
}

This never logs the important @"aha" message, unless I would programmatically set the @"FirstLaunch" key to YES.

Is this a bug or am I overlooking something? I'm merely asking out of curiosity since using the original key in the linked thread works fine.

Community
  • 1
  • 1

1 Answers1

4

You are setting the default value to a string, but reading it as a BOOL. Change the register defaults call to be similar to this:

[configuration registerDefaults:[[NSDictionary alloc] initWithObjectsAndKeys:@YES, @"FirstLaunch"]];

You should probably be setting the FirstLaunch value to NO within that if block as well.

Hardik Thakkar
  • 15,269
  • 2
  • 94
  • 81
danielbeard
  • 9,120
  • 3
  • 44
  • 58