This is probably something really simple that I am missing, why when using the code below to access settings does the default value return as (null)
- (void)updateUIForDistanceUnits {
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSString *test = [userDefaults stringForKey:#"IDDistance"];
NSLog(#"DISTANCE: %#", test);
}
Root.plist
The default value in Root.plist is just what the UI shows if there's no value set, and you have to do the same in your code or set up [NSUserDefaults -registerDefaults] correctly in your application.
You should find that once you set the value in Settings.app NSUserDefaults will return the correct value.
Related
I have two views.In One view when i click on custom button it goes into second view,In that i have text view and that data must store in temperary memory so i used following code:
NSString *myString=txtvNote.text;
[txtvNote setText:#""];
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
if (standardUserDefaults) {
[standardUserDefaults setObject:myString forKey:#"note"];
[standardUserDefaults synchronize];
}
and when i go back on 1st view by clicking on the add button it will save into database for that i used following code:
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
NSString *val = nil;
if (standardUserDefaults)
val = [standardUserDefaults objectForKey:#"note"];
NSLog(#"Note::%#",val);
and then pass this value in insert query.
Now my problem is when i left that value blank,it takes the value which is inserted before.
sequence must be like this
//first set text to something
[txtvNote setText:#""];
//then use that text
NSString *myString=txtvNote.text;
myString is a pointer to the text in txtvNote. So if you set that text to #"" then myString will be empty as well. To preserve the value of txtvNote at the time of assignment use copy or reset the text after saving it:
NSString *myString=[[txtvNote.text copy] autorelease];
[txtvNote setText:#""];
....
That's the way NSUserDefaults works - every value is stored in your app's preferences, and like any other collection in Obj-C, it doesn't take nil values. Use instance of NSNull class or empty string. Or even better - use intermediate controller to store values and use key-value observing in your first view.
I trying to save UILabel value to NSUserDefaults. I did IBAction with this code:
-(IBAction)saveData:(id)sender {
NSString *resultString = label.text;
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setObject:resultString forKey:#"result"];
[prefs synchronize];
}
Then, I connect it to button with Touch Up Inside.
What log shows after I pressed the button:
result = 0;
When I pressed a second time, then it works.
result = "28.34";
What I'm doing wrong and how can I retrieve a result?
EDIT
With this code I display result in log. I put it to same action.
NSLog(#"%#", [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]);
Your code looks correct, so the issue is probably not the function you posted but rather the code that contains your NSLog. You might be logging the value in a way that misses the first time it gets set.
Your log statement should be
NSLog(#"%#",[[NSUserDefaults
standardUserDefaults]
objectForKey:#"result"]);
This should be after you set the object in NSUserDefaults.
Add this to your applicationDidFinishLaunching in appDelegate or in init method in viewController(you have to create one) :
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
if(prefs == nil)
{
[prefs setObject:#"anything" forKey:#"result"];
[prefs synchronize];
}
I have the following code in my initialization (first time) of my app:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *uid=#"1";
[defaults setObject:uid forKey:#"init_val"];
[defaults synchronize];
Much later in the code (in response to a button press) I check the value using:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *initVal=[defaults objectForKey:#"init_val"];
initVal is always nil. I have checked and init_val is set up exactly the same in my settings bundle as another field that I can set and read from without issue (they are both set up with a single field named "Key".
#mlewis54:
You can try this code. I am very sure that it will work for you.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *uid=#"1";
[defaults setObject:uid forKey:#"init_val"];
[defaults synchronize];
For Retrieving Data You Should Use The Following Code :
NSString *initVal=[[NSUserDefaults standardUserDefaults] valueForKey:#"init_val"];
OR
NSString *initVal=[[NSUserDefaults standardUserDefaults] objectForKey:#"init_val"];
EDIT:
If still it gives nil, then you can use the following code:
NSString *initVal=[NSString stringWithFormat:#"%#",[[NSUserDefaults standardUserDefaults] valueForKey:#"init_val"]];
OR
NSString *initVal=[NSString stringWithFormat:#"%#",[[NSUserDefaults standardUserDefaults] objectForKey:#"init_val"]];
Hope this helps you.
Decelare this at an event on click of button ...
NSUserDefaults *savedData = [NSUserDefaults standardUserDefaults];
[savedData setObject:userEmailTextField.text forKey:#"userid"];
and on load load the data and check the value you have entered is showing or not...
userEmailTextField.text =[[NSUserDefaults standardUserDefaults]objectForKey:#"userid"];
You need to use the line
NSString *initVal = [defaults stringForKey:#"init_val"];
- since it's a String you must use stringForKey. You can similarly use boolForKey to get a boolean value.
There's nothing wrong in your code, i tried to copy it in my project and it worked good,
so your error could be probably when you check initVal...
what's the code you use to see if initVal is nil?
and are you sure that when/where you check initVal, that var/oblect is still visible ?
try to insert this immediately after your reading code:
NSLog(#"____text read in pref: %#", initVal);
it writes this in my console window -> __text read in pref: 1
of course it's an NSString, i hope you keep it in mind it's not an int or a NSNumber when you check it...
luca
It might be that the initialisation isn't happening. Put an NSLog in there to check it's executed.
It works fine for me with the same code:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *uid=#"1";
[defaults setObject:uid forKey:#"init_val"];
[defaults synchronize];
NSUserDefaults *defaultss = [NSUserDefaults standardUserDefaults];
NSString *initVal=[defaultss objectForKey:#"init_val"];
NSLog(#"Value of initVal: %#",initVal);
O/P on console: Value of initVal: 1
So try to do it with the same code with print on console.
Are you sure you properly initialize NSUserDefaults? When I initialize, in my app delegate, I always create the default user defaults by creating a dictionary of the default values then:
[[NSUserDefaults standardUserDefaults] registerDefaults:appDefaults];
Where appDefaults is the dictionary. Then you should be able to access them correctly.
I am getting user details from .net web server.
I place all these in an array.
Now i need to place these array values in NSuserdefaults.
i try like this.
to store
NSUserDefaults *prefs=[NSUserDefaults standardUserDefaults];
[prefs setObject:resultData forKey:#"agentinfo"];
[prefs synchronize];
to get
NSUserDefaults *prefs=[NSUserDefaults standardUserDefaults];
[prefs arrayForKey:#"agentinfo"]
i am getting details like this
970,
Aditya2,
B,
JNTU1,
"Ram#gamil.com"
how can i get individual numbers 970,Aditya2...
Because i need to Assign them to label.
can any one pls help me.
Thank u in advance
stringForKey will always return nil if the object associated with the provided key is no string. Try arrayForKey instead.
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
if (standardUserDefaults)
yourArray = [standardUserDefaults objectForKey:#"agentinfo"];
use this code for access the array.
I followed the tutorial: http://useyourloaf.com/blog/2010/5/18/adding-a-settings-bundle-to-an-iphone-app.html
And the "Shuffle Switch" (that I just created based on the tutorial) was not in the Settings App. Every time I did an NSLog on the state of the switch, it would return "(null)". The "Slideshow Switch" (which was created the same way) worked fine.
My Settings bundle Root.plist file looks as follows: (copy link and paste into web browser)
i.imgur.com/kb8DT.png
Please help as I need to create, and access a Toggle Switch created in the .plist file. I am new to iPhone Programming.
Here's the code I'm using to set the user preference switch:
// Set the application defaults
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSDictionary *appDefaults = [NSDictionary dictionaryWithObject:#"YES" forKey:#"ShuffleToggleKey"];
[defaults registerDefaults:appDefaults];
[defaults synchronize];
And here's the code I'm using to get the state of the user preference switch:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
BOOL enabled = [defaults boolForKey:#"ShuffleToggleKey"];
It seems, that you're putting string object and trying to get boolean value.
You should or get out the string like
NSString *enabledStr = [defaults stringForKey:#"ShuffleToggleKey"];
BOOL enabled = [enabledStr boolValue];
or put a boolean value in the first place like that:
[defaults setBool:YES forKey:#"ShuffleToggleKey"];
Then you can retrieve it as
BOOL enabled = [defaults boolForKey:#"ShuffleToggleKey"];