52

Hi I am trying to use NSUserDefaults to save some default values in database. I am able to save the values in the NSUserDefaults (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 NSUserDefaults. Following is my code from my class where I save the values in NSUserDefaults:

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?

KlimczakM
  • 12,576
  • 11
  • 64
  • 83
pankaj
  • 7,878
  • 16
  • 69
  • 115

4 Answers4

208

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

If you terminate your app by pressing "Stop" in Xcode (in the Simulator or on the device), your NSUserDefaults 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];



Addendum:

In iOS4 (this answer was originally written when iOS3 was the public release), your NSUserDefaults may not get saved when pressing the home button. Manually calling [[NSUserDefaults standardUserDefaults] synchronize] in applicationDidEnterBackground: should ensure that your NSUserDefaults are saved correctly (this should really be a built-in behaviour IMO).

KlimczakM
  • 12,576
  • 11
  • 64
  • 83
Nick Forge
  • 21,344
  • 7
  • 55
  • 78
  • 1
    iOS 8 (iPad mini), I'm having this exact problem, and `synchronize` in both `appWillTerminate` and `appDidEnterBackground` is not helping. Gah!!! – Olie Mar 08 '15 at 19:19
  • 1
    According to the comments above `synchronize()` in Apple's source code file `NSUserDefaults.h`: "`-synchronize` is deprecated." – ma11hew28 Aug 22 '16 at 16:02
  • For the record, the current (2020) documentation for NSUserDefaults states: `-synchronize: Waits for any pending asynchronous updates to the defaults database and returns; this method is unnecessary and shouldn't be used.` – Tad Jan 16 '20 at 12:00
  • I believe I am hitting this same scenario (command-period in xcode does not save the user prefs) and it is 2023 and I'm on iOS16 – esilver May 03 '23 at 03:39
10

This code works fine for me .

NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];

if (standardUserDefaults) {
    [standardUserDefaults setObject:myString forKey:@"Prefs"];
    [standardUserDefaults synchronize];
}
Kannan Prasad
  • 1,796
  • 22
  • 27
  • 8
    This answer doesn't really add anything to the discussion; the code snippets are already available up-thread. – Freney Oct 26 '12 at 11:57
2

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
  • 9,212
  • 2
  • 29
  • 38
  • 2
    I don't believe this is correct. (It may have been with an older version of the sdk.) – Brad The App Guy Aug 29 '11 at 17:50
  • 2
    I agree with Brad. Simulator retains preference values during its relaunch from xcode. It only loses preferences when we delete the application from simulator. – pankaj Sep 15 '11 at 12:11
  • 3
    The current version of the simulator (iOS 4) retains preferences so my answer is not longer applicable. It was originally written when iOS 3 were current. – Claus Broch Sep 15 '11 at 16:58
1

In my case I was saving and retrieving a string. When I synchronized after saving and then retrived in another thread, it was not working properly. The problem was solved by synchronizing both after saving and before retreiving.

juan Isaza
  • 3,646
  • 3
  • 31
  • 37