views:

96

answers:

3

Hi there

I have this in my app delegate applicationDidFinishLaunching method:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if([defaults objectForKey:@"AcceptTC"] == nil){
    NSDictionary *appDefaults = [NSDictionary dictionaryWithObject:@"NO" forKey:@"AcceptTC"];
    [defaults registerDefaults:appDefaults];
}

and I have this in my RootViewController viewDidLoad method:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if(![defaults boolForKey:@"AcceptTC"]){
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Notice" message:@"By using this application you agree to be bound by the Terms and Conditions as stated within this application." delegate:self cancelButtonTitle:@"No Deal" otherButtonTitles:@"I Understand",nil];
    [alert show];
    [alert release];
}

and my alert view delegate does this:

if(buttonIndex == 0){
            exit(0);
        }
        else{
            NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
            [defaults setObject:@"YES" forKey:@"AcceptTC"];
        }

However when I click "I understand" (button index 1) and then restart the application I still see the alert view! Even though I've definiely set the value to YES.

I have no idea how to change this. :( I only want it to show the first time a user starts the application - i don't want to keep showing it every time they want to use it.

Thanks

+1  A: 

Concering registerDefaults:

The contents of the registration domain are not written to disk; you need to call this method each time your application starts. You can place a plist file in the application's Resources directory and call registerDefaults: with the contents that you read in from that file.

// Load default defaults
[[NSUserDefaults standardUserDefaults] registerDefaults:[NSDictionary \
    dictionaryWithContentsOfFile:[[NSBundle mainBundle] \ 
    pathForResource:@"Defaults" ofType:@"plist"]]];

Code taken from this SO answer.

Another blog article about NSDefaults:

The MYYN
right... but i don't want to set the defaults each time the application loads. I want to set the default to NO and then never set it again unless the user clicks "I understand" and then I want to set it to "YES" and leave it forever - like settings. You don't put in your facebook login details EVERY TIME you open the app do you? Or you don't have to reset settings every time you open another app?
Thomas Clayson
Alright, have you tried something like `[[NSUserDefaults standardUserDefaults] synchronize];`
The MYYN
was missing synchronize.. :p didn't realise i had to do that! Thank you. :)
Thomas Clayson
+1  A: 

Maybe you need to call synchronize on the defaults to save the changes to disk?

zoul
was missing synchronize.. :p didn't realise i had to do that! Thank you. :)
Thomas Clayson
+1  A: 

In my application I'm using NSUserDefaults with a bool, works fine.

When the first ViewController loads, it will do:

BOOL terms = [[NSUserDefaults standardUserDefaults] boolForKey:@"termsaccepted"];

if (!terms) {
   [self presentModalViewController:disclaimerViewController animated:YES];
}

Within the disclaimer view, after the button has been tapped:

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

I think you're missing the "synchronize" part. However I find using a bool more streamlined, too.

Toastor
was missing synchronize.. :p didn't realise i had to do that! Thank you. :)
Thomas Clayson