Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is NSUserDefaults not saving my values?

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?

like image 816
pankaj Avatar asked Apr 12 '10 14:04

pankaj


People also ask

How much data can you store in NSUserDefaults?

It appears the limit is the maximum file size for iOS (logically), which is currently 4GB: https://discussions.apple.com/thread/1763096?tstart=0. The precise size of the data is circumscribed by the compiler types (NSData, NSString, etc.) or the files in your asset bundle.

How do you save NSUserDefaults in Objective C?

static func setObject(value:AnyObject ,key:String) { let pref = NSUserDefaults. standardUserDefaults() pref. setObject(value, forKey: key) pref. synchronize() } static func getObject(key:String) -> AnyObject { let pref = NSUserDefaults.

How do you check if NSUserDefaults is empty in Objective C?

There isn't a way to check whether an object within NSUserDefaults is empty or not. However, you can check whether a value for particular key is nil or not.

How do I save an image in UserDefaults?

It is, but it isn't possible to store an image as is in the user's defaults database. The defaults system only supports strings, numbers, Date objects, and Data objects. This means that you need to convert the image to a Data object before you can store it in the user's defaults database.


2 Answers

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).

like image 112
Nick Forge Avatar answered Sep 18 '22 21:09

Nick Forge


This code works fine for me .

NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];  if (standardUserDefaults) {     [standardUserDefaults setObject:myString forKey:@"Prefs"];     [standardUserDefaults synchronize]; } 
like image 41
Kannan Prasad Avatar answered Sep 18 '22 21:09

Kannan Prasad