Question

I want to open a ViewController only once when the app start for the very first time. Below is my code. The problem is when I am writing YES to NSUserdefaults , doing a synchronise , and then closing the app. in Xcode using simulator , the value is not updated to Yes.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"isFirstLaunch"])
    {
        // app already launched
        [[NSUserDefaults standardUserDefaults] registerDefaults:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:NO],@"isFirstLaunch", nil]];
        [[NSUserDefaults standardUserDefaults]synchronize];
    }

    else
    {
        [[NSUserDefaults standardUserDefaults] registerDefaults:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES],@"isFirstLaunch", nil]];
        [[NSUserDefaults standardUserDefaults]synchronize];
    }

    return YES;
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
   if ([[NSUserDefaults standardUserDefaults] boolForKey:@"isFirstLaunch"])
   {
            NSLog(@"first time");
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    LASplashUserSettingViewController *splash = [storyboard instantiateViewControllerWithIdentifier:@"splash"];
    [self.window.rootViewController presentViewController:splash animated:NO completion:nil];//
   }

}

Why is NSUserDefaults not saving my values?

Have followed above and many other links. what am i missing here? Thanks,

Was it helpful?

Solution

what am i missing here?

-registerDefaults: doesn't do what you think it does. Use -setBool:forKey: instead.

-registerDefaults: is used to ensure that values exist for certain keys in the defaults system. There's no need to call -synchronize after that method because the values in the registration domain are never saved. You probably meant to use that method to set up the default value YES for isFirstLaunch, but your code checks the value for the key before you register the default value! which defeats the purpose of the method. Proper use would be to call -registerDefaults: first, and then check the value associated with the key, and if the value is YES then call -setBool:forKey: to save the value NO.

A simpler option to detect the first launch would be to invert the question. Skip the -registerDefaults: call altogether and change the key to appHasRunPreviously. If no value is stored for a given key, -boolForKey: will return NO. If that happens, you know that this is the first time the app is running, and you should save YES for that key using -setBool:forKey:.

OTHER TIPS

This is how you use NSUserDefaults in AppDelegate.m

+ (void)initialize 
{
    // Register defaults for first time
    NSMutableDictionary *defs = [NSMutableDictionary dictionary];
    [defs setObject:[NSNumber numberWithBool:YES] forKey:@"isFirstLaunch"];
    [[NSUserDefaults standardUserDefaults] registerDefaults:defs];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    if([defaults boolForKey:@"isFirstLaunch"])
    {
        // Do your stuff here

        [defaults setBool:NO forKey:@"isFirstLaunch"];
        [defaults synchronize];
    }
}

Don't set isFirstLaunch back to YES in the else-condition if you only want the view to be shown once.

You can try this

 [[NSUserDefaults standardUserDefaults]setBool:YES forKey:@"AlreadyLaunched"];


 [[NSUserDefaults standardUserDefaults]boolForKey:@"AlreadyLaunched"]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top