tags:

views:

770

answers:

2

hi i am trying to use nsuser defaults to save some default values in database. I am able to save the values in the nsuserdefault(even checked it in nslog). Now i need the values in app delegate when the application is restarted. But i am not getting anything in the nsuserdefault. Following is my code from my class where i save the values in nsuserdefault:

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

            [prefs setObject:appDel.dictProfile forKey:@"dict"];
            NSLog(@"%@",[prefs valueForKey:@"dict"]);

Following is my code from App Delegagte:

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

NSLog(@"%@",[prefs valueForKey:@"dict"]);

the above code always returns me null. Can some one please help me

A: 

You didn't say whether you are running on a device or in the simulator, but if you restart the application in the simulator, all preferences will be reset between launches if you launch from Xcode. The preferences will only be preserved if you relaunch from the simulator itself.

Claus Broch
+7  A: 

If you terminate your app by pressing the home button (in the Simulator or on the device), your User Defaults will get saved.

If you terminate your app by pressing "Stop" in Xcode (in the Simulator or on the device), your User Defaults might get saved, but there's a good chance they won't. NSUserDefaults persists any changes periodically, and if you terminate the process before they've been persisted, they'll be gone. You can force the save by calling:

[[NSUserDefaults standardUserDefaults] synchronize];
Nick Forge
thanks! that drove me nuts! :)
Henrik P. Hessel