have to save UITextfield's value to the NSUserDefault - iphone

I am a new comer to the iPhone world, I am having a question,I am having a login page in which 2 levels and associated 2 uitextfield,i have to save both the UITextfield's Values(inputt by user) to the NSUserDefault,and new time when we start application both the UITextfield should be filled with the values we hav input before....i am in bid trouble
code would be appreciated
Thanx in advance

-(void)saveLoginToUserDefaults:(NSString*)loginString
{
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
if (standardUserDefaults) {
[standardUserDefaults setObject:loginString forKey:#"Login"];
}
}
-(NSString*)retrieveLoginFromUserDefaults
{
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
NSString *val = nil;
if (standardUserDefaults)
val = [standardUserDefaults objectForKey:#"Login"];
return val;
}

Related

Restoring value in NSUserDefault

I have stored my initial set up values using NSUserDefault like this...
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:is_remember_chkd forKey:remember_me.titleLabel.text]; // Button text as key
[defaults setBool:is_signin_auto_chkd forKey:signin_automatic.titleLabel.text];
[defaults setBool:is_signin_secret_chkd forKey:signin_secret.titleLabel.text];
[defaults synchronize];
And I retrieved as ....
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
is_remember_chkd = [defaults objectForKey:remember_me.titleLabel.text];
is_signin_auto_chkd = [defaults objectForKey:signin_automatic.titleLabel.text];
is_signin_secret_chkd = [defaults objectForKey:signin_secret.titleLabel.text];
But I am not getting the last value, Am I doing anything wrong.
For retrieved the NSUserDefaults Value , replace the objectForKey to boolForKey .
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
is_remember_chkd = [defaults boolForKey:remember_me.titleLabel.text];
is_signin_auto_chkd = [defaults boolForKey:signin_automatic.titleLabel.text];
is_signin_secret_chkd = [defaults boolForKey :signin_secret.titleLabel.text];
This should do the trick for you, extract boolValue from the returned value
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
is_remember_chkd = [[defaults objectForKey:remember_me.titleLabel.text] boolValue];
is_signin_auto_chkd = [[defaults objectForKey:signin_automatic.titleLabel.text] boolValue];
is_signin_secret_chkd = [[defaults objectForKey:signin_secret.titleLabel.text] boolValue];
hope it helps. happy coding :)

what is the use of standardUserDefaults?

I am new in iphone programming. please explain me this code. and that is the use of standardUserDefaults? how this code will work?
-(void)load
{
DLog("Load Configuration");
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
bool savedDefaults = [defaults boolForKey:kKeySavedDefaults];
if (savedDefaults)
{
columns = [defaults integerForKey:kKeyColumns];
if (columns == 0) columns = kColumnsDefault;
rows = [defaults integerForKey:kKeyRows];
if (rows == 0) rows = kRowsDefault;
photoType = [defaults integerForKey:kKeylastPhotoType];
photoEnabled = [defaults boolForKey:kKeyPhotoEnabled];
numbersEnabled = [defaults boolForKey:kKeyNumbersEnabled];
soundEnabled = [defaults boolForKey:kKeySoundEnabled];
}
else
{
columns = kColumnsDefault;
rows = kRowsDefault;
photoType = klastPhotoTypeDefault;
photoEnabled = kPhotoEnabledDefault;
numbersEnabled = kNumbersEnabledDefault;
soundEnabled = kSoundEnabledDefault;
}
}
-(void)save
{
DLog("Save Configuration");
BOOL restart = NO;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults integerForKey:kKeyColumns] != columns) { restart = YES; }
if ([defaults integerForKey:kKeyRows] != rows) { restart = YES; }
[defaults setBool:YES forKey:kKeySavedDefaults];
[defaults setInteger:columns forKey:kKeyColumns];
[defaults setInteger:rows forKey:kKeyRows];
[defaults setInteger:photoType forKey:kKeylastPhotoType];
[defaults setBool:photoEnabled forKey:kKeyPhotoEnabled];
[defaults setBool:numbersEnabled forKey:kKeyNumbersEnabled];
[defaults setBool:soundEnabled forKey:kKeySoundEnabled];
[defaults synchronize];
[board configChanged:restart];
}
User defaults are used to store little configuration parameters.
From the documentation :
The NSUserDefaults class provides a programmatic interface for
interacting with the defaults system. The defaults system allows an
application to customize its behavior to match a user’s preferences.
For example, you can allow users to determine what units of
measurement your application displays or how often documents are
automatically saved. Applications record such preferences by assigning
values to a set of parameters in a user’s defaults database. The
parameters are referred to as defaults since they’re commonly used to
determine an application’s default state at startup or the way it acts
by default.
If you want to store some small amount of data like username,password...... by using these concept you can store the values

How do I save a UISlider value?

How could I save the value of a UISlider ?I tried this, but it didn't work:
-(IBAction)save {
slider1i = slider1.value;
NSUserDefaults *slider1save = [NSUserDefaults standardUserDefaults];
[slider1save setInteger:slider1i forKey:#"slider1key"];
[slider1save synchronize];
}
slider1i is a NSinteger, and slider1 is the UISlider ...
Can you help-me ?
As user 698952 said, UISlider's value property is a float. But i'd instead use setFloat like this:
slider1i = slider1.value;
NSUserDefaults *slider1save = [NSUserDefaults standardUserDefaults];
[slider1save setFloat:slider1i forKey:#"slider1key"];
[slider1save synchronize];
slider1.valueis float value so you first need to convert it in NSString or NSNumber inorder to save it ..... that makes it a object
-(IBAction)save {
NSNumber *sliderValue = [NSNumber numberWithFloat:slider1.value];
NSUserDefaults *slider1save = [NSUserDefaults standardUserDefaults];
[slider1save setObject:sliderValue forKey:#"slider1key"];
[slider1save synchronize];
}
thanks

How to store a float in NSUserDefaults

I want to store a float value into NSUserDefaults.
I also need to check that the float value exists..if not I need to assign some value in it.
and retrieve it...for the above I have the below code but it gives me an error.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults boolForKey:#"HANDWRITING_SIZE_SLIDER"] == YES) {
self.sizeSlider.value = 10.0;
} else {
self.sizeSlider.value = [[NSUserDefaults standardUserDefaults] floatForKey:#"HANDWRITING_SIZE_SLIDER"]];
}
Thanks for any help
Use the NSNumber class for this and store it via the setObject:forKey: method so you can check if it exists.
I'd also suggest the usage of constants as keys:
#define HANDWRITING_SIZE_SLIDER #"HSS"
Your code should be along these lines:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:HANDWRITING_SIZE_SLIDER] == nil) {
//doesn't exist in NSUserDefaults, set to default value...
self.sizeSlider.value = 10.0;
} else {
self.sizeSlider.value = [[defaults objectForKey:HANDWRITING_SIZE_SLIDER] floatValue];
}
Somewhere else in your app, you'd set the value in NSUserDefaults like this:
float sizeSliderValue = ...
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithFloat:sizeSliderValue] forKey:HANDWRITING_SIZE_SLIDER];
Try this code:
-(IBAction)sliderAction:(id)sender
{
float sliderValue = [sender floatValue];
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithFloat:sliderValue] forKey:#"keySliderValue"];
NSLog(#"%#",[[NSUserDefaults standardUserDefaults] objectForKey:#"keySliderValue"]);
}

Iphone SDK how to save my score?

i have a integer called HighScore which is connected to highscorelabel . i have made it so when the user gets a high score it puts the score they got onto the label but i would now like to know how i can save it so that when the app is opened again it will still have the high score : this is my code for detecting when a high score is made
(void) submitScore {
if (lives > HighScore){
HighScore = lives;
}
highscorelabel.text = [NSString stringWithFormat:#"%i" , HighScore];
}
Use NSUserDefaults for saving:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setInteger:HighScore forKey:#"HighScore "];
[prefs synchronize];
and retrieving:
HighScore = [prefs integerForKey:#"HighScore"];