I am trying to find out how to save/store my values from NSDefaults so that when I exit the application they are stored in the Settings.bundle. This is what I am doing...
NSDictionary *settings = [NSDictionary dictionaryWithObjectsAndKeys:#"M1", #"IDMissiles",
#"G2", #"IDGuns",
#"B3", #"IDBombs",
#"KM", #"IDDistance", nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:settings];
If I do the following, the values print out correctly from NSUserDefaults ...
NSLog(#"IDMissiles: %#", [userDefaults stringForKey:#"IDMissiles"]);
NSLog(#"IDGuns : %#", [userDefaults stringForKey:#"IDGuns"]);
NSLog(#"IDBombs : %#", [userDefaults stringForKey:#"IDBombs"]);
NSLog(#"IDDistance: %#", [userDefaults stringForKey:#"IDDistance"]);
However ... Each time I run the application the values in NSUserDefaults start off as (null), I was thinking that doing [[NSUserDefaults standardUserDefaults] synchronize]; would store the values for the next time I run the application, but no such luck.
Instead of using
[[NSUserDefaults standardUserDefaults] registerDefaults:settings];
try this:
[[NSUserDefaults standardUserDefaults] setObject:settings forKey:#"settings"];
Then, get from defaults like this:
NSLog(#"IDMissiles:%#[[[NSUserDefaultsstandardUserDefaults]objectForKey:#"settings"]objectForKey:#"IDMissiles"]);
One thing I discovered when working with the settings.bundle is that none of the values get initialized until you actually open the settings pane. You can have default values saved there, but they will return nil until you open the settings.
I'm not sure if this happens when you try and save values there but never open the settings pane.
If you are not using a settings pane, then you wouldn't want to use the registerDefaults option.
Try this instead.
[[NSUserDefaults standardDefaults] setObject:#"M1" forKey:#"IDMissiles"];
// set remaining values
[[NSUserDefaults standardDefaults] synchronize]; // this really only needs to be called if you plan on accessing values right away, otherwise they are saved automatically after the next run loop
From the documentation:
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.
In other words, you aren't storing anything by registering defaults like this. To have default values both in your app and in the settings bundle, you have to maintain the settings bundle separately as discussed here.
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[NSUserDefaults removeObjectForKey:#"userDefaults"];
[userDefaults setObject:[settings objectForKey:#"mainData"] forKey:#"userDefaultsValue"];
[userDefaults synchronize];
Related
I have a app and I connect with Json to a mysql DB using PHP and return a value ( UserID ). I now would like to use that value returned for other JSON calls in my WHERE statements.
Do i store the value in a plist file?
How do i make that a global value for that specific user?
Thanks
All you need to do is to save this value in your NSUserDefaults:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
// write value
[defaults setInteger:value forKey:#"value"];
[defaults synchronize];
// read
// if value is not an int but an NSNumber, change it to [defaults objectForKey:#"value"]
value = [defaults intForKey:#"value"];
Historically on iOS, the device UDID has been used as a user's ID in databases. That's recently been banned by Apple, so unless you're forcing users to register with a username and password, you should use the vendor ID as their user ID in the database. The vendor ID is determined using:
NSString* vendorID = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
You should pass this value to the server and insert it into your user table as the user's ID.
You can use NSUserDefault to store any variable that is common throughout the application. You can add the object in it and you can remove that when application terminates or that particular User logs out from the application.
e.g.
[[NSUserDefaults standardUserDefaults]setObject:#"10" forKey:#"UserID"];
[[NSUserDefaults standardUserDefaults]synchronize];
And to remove
[[NSUserDefaults standardUserDefaults]removeObjectForKey:#"UserID"];
Playing with my code today,
I run this particular piece of code several times in minor variations throughout a particular class, I'm trying to streamline though. The difference in effect is minimal but changes the amount of code by a volume of hundreds or thousands of lines so would be a big personal win for me.
Essentially I have a value stored as an integer with a key of 'codeKey' and I want to insert the value of that key where the number 30061 currently resides. I'm at a bit of a loss how, can anyone help me out with this one?
I know I need to recall the value somehow and place it in but I'm not really sure how that would look.
if ([[NSUserDefaults standardUserDefaults] integerForKey:#"buttonID"] == 1) {
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setInteger:30061 forKey:#"scifi1"];
[userDefaults synchronize];}
I take it you mean dynamically saving this information without duplicating the same code over and over. If that is correct, your solution will be something like this:
-(void)saveCodeKey:(int)key {
if ([[NSUserDefaults standardUserDefaults] integerForKey:#"buttonID"] == 1) {
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setInteger:key forKey:#"scifi1"];
[userDefaults synchronize];
}
}
Now you can simply invoke [self saveCodeKey:12345]; Assuming the -saveCodeKey: method resides in the same class.
Hope this helps !
I know there are thousands of tutorials on the topic however I don't get it to work.
What I am trying to do is to read an value from a plist file. Then I check whether or not I have to update the value. This part is ok. However I don't managed to get the value saved in plist file. I have a Settings.Bundle and it is where I am trying to read/store.
It is just a simple string.
Thanks in advance
The following will give you a NSDictionary with your settings bundle information.
[[NSBundle mainBundle] infoDictionary]
If there are any other generic plist you would like to read you can easily load them into a NSDictionary using
[NSDictionary dictionaryWithContentsOfFile:filePath];
//or
[[NSDictionary alloc] initWithContentsOfFile:filePath]
Also I would like to add for reading and saving values I would recommend doing that in the NSUserDefaults
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
id anObj = [userDefaults objectForKey:#"myKey"];
//Modify anObj
...
[userDefaults setObject:anObj forKey:#"myKey"];
[userDefaults synchronize];//Save immediately if you choose
You cannot modify files/folders present in your app bundle (such as Settings.bundle.) They are read-only.
I read Apple Programming Guide on this subject but couldn't figure it out.
I created a Settings bundle using the following tutorial, and I tried accessing my preferences (edited manually) like this:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[self setShouldPlaySounds:[defaults boolForKey:#"play_sounds_preference"]];
for "Key" I used the key value entered in the xml editor (double click on Root.plist).
I know you can build preferences with "Identifier" key and "DefaultValue" but I don't want the settings to be accesible in the setting app, I just want two dictuinaries with some strings for my custom settings.
What am I doing wrong? Why can't I get the value of the preferences?
Is it simpler to create my own config file? Implementing a serializer.
Are you setting the value anywhere? And what behavior are you seeing with respect to the value of shouldPlaySounds?
What happens if you execute this first?
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:YES forKey:#"play_sounds_preference"];
BOOL result = [defaults synchronize]; // force immediate saving of defaults.
NSLog(#"[defaults synchronize] returned %d", result);
After uninstalling an application completely from the device and then loading it in the debugger, I am attempting in a setup method to load a flag using boolForKey. The first time the app runs I have the expectation that the bool will not exist, since I have just reinstalled the app. I expect from the documentation that boolForKey will therefore return NO.
I am seeing the opposite though. boolForKey is returning YES, which fubars my initial user settings. Any idea why this might be happening or a good way around it?
BOOL stopAutoLogin = [[NSUserDefaults standardUserDefaults] boolForKey:#"StopAutoLogin"];
_userWantsAutoLogin = !stopAutoLogin;
So stopAutoLogin comes out as "YES", which is completely unexpected.
Stranger and stranger: When I call objectForKey:#"StopAutoLogin" I get a nil object, as expected. It's just the boolForKey that returns a bad value. So I changed the code to this:
// this is nil
NSObject *wrapper = [[NSUserDefaults standardUserDefaults] objectForKey:#"StopAutoLogin"];
// this is YES
BOOL stopAutoLogin = [[NSUserDefaults standardUserDefaults] boolForKey:#"StopAutoLogin"];
please try [UserDefaults synchronize];
Because this method is automatically invoked at periodic intervals, use this method only if you cannot wait for the automatic synchronization (for example, if your application is about to exit) or if you want to update the user defaults to what is on disk even though you have not made any changes.
please see: http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/Reference/Reference.html
Do you register the default values for your keys?
NSMutableDictionary *appDefaults = [NSMutableDictionary dictionaryWithCapacity:1];
[appDefaults setObject:#"NO" forKey:kReloadOnStartKey];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults registerDefaults:appDefaults];
If there is no registration domain,
one is created using the specified
dictionary, and NSRegistrationDomain
is added to the end of the search
list.
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.
See this link for more information.