Saving and displaying data with NSUserDefaults - iphone

I've saved some input from a UITextField using the following code:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:myTextField.text forKey:#"myTextFieldKey"];
[defaults synchronize];
I'd like to display this saved text on a UILabel in another view controller.
I tried this:
myLabel.text = [[NSUserDefaults standardUserDefaults] objectForKey:#"myTextFieldKey"];
but nothing displays. Any help with this would be great. thanks.

Well the loading and saving code is correct, so it looks like the problem is something else.
Try this to debug:
NSString *aValue = [[NSUserDefaults standardUserDefaults] objectForKey:#"myTextFieldKey"];
NSLog(#"Value from standardUserDefaults: %#", aValue);
NSLog(#"Label: %#", myLabel);
myLabel.text = aValue;
Now you will see if the value retriever from the NSUserDefaults is set and if the label is assinged.

[[NSUserDefaults standardUserDefaults] setValue:myTextField.text forKey:#"myTextFieldKey"];
[[NSUserDefaults standardUserDefaults] synchronize];
After that use valueForKey not objectForKey:
myLabel.text = [[NSUserDefaults standardUserDefaults] valueForKey:#"myTextFieldKey"];

Try:
myLabel.text = [[NSUserDefaults standardUserDefaults] stringForKey:#"myTextFieldKey"];

Check that myTextField and myLabel aren't nil.

Related

leaving the uiswitch on

I think i have followed the instructions for leaving the uiswith on or off after someone has logged in or out of the app. I know i am missing something...
Also on the
However it is not saving. Also I don't get any errors compiling, just when i switch views, it looses the stored value.
Thank you in advance
-(IBAction)hintsPressed:(UISwitch *)sender {
BOOL value = [sender isOn];
[((FirstAppAppDelegate*)MI_SHARED_DELEGATE) setHintsOnorOff:&value];
if ([((FirstAppAppDelegate*)MI_SHARED_DELEGATE) hints]==NO) {
[[NSUserDefaults standardUserDefaults] setValue:[NSNumber numberWithBool:NO] forKey:hintsOnOff];
}else if([((FirstAppAppDelegate*)MI_SHARED_DELEGATE) hints]==YES) {
[[NSUserDefaults standardUserDefaults] setValue:[NSNumber numberWithBool:YES] forKey:hintsOnOff];
}
}
Try this lines of code
-(IBAction)hintsPressed:(UISwitch *)sender
{
BOOL value = [sender isOn];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setValue:[NSNumber numberWithBool:value] forKey:hintsOnOff];
[defaults synchronize];
}
May this will solve your problem :)
Try this:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
// your code, [defaults setValue: ...
[defaults synchronize];
It's that last step that persists the defaults.

retrieve values from NSUserDefault in iPhone after setting again

In App delegate, I have following code:
NSUserDefaults *pref = [NSUserDefaults standardUserDefaults];
NSString *alrmTime = #"10:00 AM";
[pref setObject:alrmTime forKey:#"alarmTime"];
[prefs synchronize];
From here I am getting from App delegate User Daeault in Controller A using code
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *tempAlarmTime = [defaults stringForKey:#"alarmTime"];
cell.textLabel.text = [NSString stringWithFormat:#"Remind At %#", tempAlarmTime];
Now, I need to set this userdefault in Controler B , For this m using this:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setObject:mTimeIntervalSTR forKey:#"alarmTime"];
[prefs synchronize];
Now when I need to get this new value in Controller A its coming Null. Why this is happening, and how will I get new set value?
in Controler B use the following to change the userdefault value
NSString *timeString =[[[NSUserDefaults standardUserDefaults] objectForKey:#"alarmTime
"]mutableCopy];
timeString = mTimeIntervalSTR;
[[NSUserDefaults standardUserDefaults]setObject:timeString
forKey:#"alarmTime "];
[[NSUserDefaults standardUserDefaults]synchronize];
After adding a value, call [[NSUserDefaults standardUserDefaults] synchronize];.

Save an int value, and load then app loads

I'm making an app that changes images every time the user press on the screen. It is using an int to know then to change image.
I need help to save the int value then screen is pressed and load it then the app is loaded.
You could use the NSUserDefaults class for this:
// Write
[[NSUserDefaults standardUserDefaults] setInteger:7615 forKey:#"customIntegerValue"];
[[NSUserDefaults standardUserDefaults] synchronize];
// Read
NSLog(#"Integer = %i", [[NSUserDefaults standardUserDefaults] integerForKey:#"customIntegerValue"];
// To save in pref
int highScore = yourGameScore;
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:highScore] forKey:#"HighScore"];
[[NSUserDefaults standardUserDefaults] synchronize];
// To get saved value from pref
highScore = [[[NSUserDefaults standardUserDefaults] objectForKey:#"HighScore"] intValue ];
You can see similar post here: stackOldPost
You can save values in NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

Detecting the first ever run of an app

I am creating an app in which I have to create a plist when the app launches for the first time. I'm later going to use the plist to store details a user later inputs. How can I detect the first launch of the app? I was experimenting with NSUserDefaults but I think I'm doing something wrong.
You can do this with NSUserDefaults. But be careful with the Version Number.
Do the following:
NSString *bundleVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:(NSString *)kCFBundleVersionKey];
NSString *appFirstStartOfVersionKey = [NSString stringWithFormat:#"first_start_%#", bundleVersion];
NSNumber *alreadyStartedOnVersion = [[NSUserDefaults standardUserDefaults] objectForKey:appFirstStartOfVersionKey];
if(!alreadyStartedOnVersion || [alreadyStartedOnVersion boolValue] == NO) {
[self firstStartCode];
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithBool:YES] forKey:appFirstStartOfVersionKey];
}
the selector firstStartCode will only be called on time for each application version on the very first run.
Okay?
I like to use NSUserDefaults to store an indication of the the first run.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if (![defaults objectForKey:#"firstRun"])
[defaults setObject:[NSDate date] forKey:#"firstRun"];
[[NSUserDefaults standardUserDefaults] synchronize];
You can then test for it later...
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if([defaults objectForKey:#"firstRun"])
{
// do something or not...
}
Taken from: Best way to check if an iPhone app is running for the first time
You can use the following:
-(void) firstLaunch {
//Code goes here
}
-(void) firstLaunchCheck {
if(![[NSUserDefaults standardUserDefaults] boolForKey:#"didLaunchFirstTime"]) {
[[NSUserDefaults standardUserDefaults] setBool:TRUE forKey:#"didLaunchFirstTime"];
[self firstLaunch];
}
}

Use NSUserDefaults to access a stored object

I want to access text saved through NSUserDefaults and display it in a label that has already been defined called "name". My code is below, but it doesn't work. What should I do? Thanks for your help!
name = [[NSUserDefaults standardUserDefaults] objectForKey:#"Name"];
should be:
name.text = [[NSUserDefaults standardUserDefaults] objectForKey:#"Name"];
First, you should register your defaults someway like this: (this is only necessary if you want to add multiple items!)
NSDictionary *defaultsDict =
[NSDictionary dictionaryWithObjectsAndKeys:#"MyName", #"defaultName", #"MyAge", #"defaultAge", nil];`
[[NSUserDefaults standardUserDefaults] registerDefaults:defaultsDict];
Now use this assuming you have a label pointer:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[label setText:[defaults stringForKey:#"defaultName"]];