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];
Related
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];.
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];
}
}
Im developing a reminder app.
So my client want to set a rate this application popup message, that'll come up on the 10th time user open the app.is this possible.
How can i implement this?
Can anyone help me please.Thanks in advance
You could use NSUserDefaults for this:
NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
NSInteger appLaunchAmounts = [userDefaults integerForKey:#"LaunchAmounts"];
if (appLaunchAmounts == 10)
{
[self showMessage];
}
[userDefaults setInteger:appLaunchAmounts+1 forKey:#"LaunchAmounts"];
You can store that into the NSUserDefaults. Just update it in applicationDidFinishLaunching:.
You can save an integer in NSUserDefaults
- (void)setInteger:(NSInteger)value forKey:(NSString *)defaultName
Retrieve it and increment it every time the appDidFinishLaunching (or appWillEnterForeground) delegate methods is called. Probably best to use appWillEnterForeground as sometimes apps can lie in the background unterminated for days.
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
NSInteger count = [defaults integerForKey:#"LaunchCount"];
count++;
/* Do checks and review prompt */
[defaults setInteger:count forKey:#"LaunchCount"];
[defaults synchronize];
This will store a value in NSUserDefaults called 'AppLaunchCount'.
- (BOOL)application:(UIApplication *)app didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if ([[NSUserDefaults standardUserDefaults] integerForKey:#"AppLaunchCount"])
{
[[NSUserDefaults standardUserDefaults] setInteger:([[NSUserDefaults standardUserDefaults] integerForKey:#"AppLaunchCount"] + 1) forKey:#"AppLaunchCount"];
}
else
{
[[NSUserDefaults standardUserDefaults] setInteger:1 forKey:#"AppLaunchCount"];
}
}
I am saving my date selected from datepicker in nuserdefaults. When I select the date from date picker say 9.52 AM and click on the save button the date selected is getting saved in userdefaults and in SQLite. If I select another date to set another alarm say 9.54 AM and click on the save button, both the dates are getting saved in SQLite but the problem is the earlier 9.52 date is getting overrided by 9.54 in nsuserdefaults and so the notification for the last added date in userdefaults is shown.
I want that if I select 9.52AM and then click save and then select 9.54 AM and click save both the time should be saved in userdefaults and the notification for 9.52AM should be shown first.
I think you add date objects to array and than add to NSUserDefaults
if ([[NSUserDefaults standardUserDefaults] valueForKey:#"date"]==nil)
{
[[NSUserDefaults standardUserDefaults] setObject:[NSDate date] forKey:#"date"];
[[NSUserDefaults standardUserDefaults] synchronize];
NSLog(#"%#",[[NSUserDefaults standardUserDefaults] objectForKey:#"date"]);
}
else
{
NSArray *array = [[NSUserDefaults standardUserDefaults] objectForKey:#"date"];
NSMutableArray *array_dates = [[NSMutableArray alloc] init];
for(int i =0;i<[array count];i++)
{
[array_dates addObject:[array objectAtIndex:i]];
}
[array_dates addObject:YourpresentdateObject];
[[NSUserDefaults standardUserDefaults] setObject:array_dates forKey:#"date"];
[[NSUserDefaults standardUserDefaults] synchronize];
NSLog(#"%#",[[NSUserDefaults standardUserDefaults] objectForKey:#"date"]);
}
If you don't want to override dates than use different keys for both the alarm.
You will have to use different keys when you save the date using NSUserDefaults. Something like-
//Use whatever format you are using to save the date
[[NSUserDefaults standardUserDefaults] setInteger:952 forKey:#"alarm1"];
[[NSUserDefaults standardUserDefaults] setInteger:954 forKey:#"alarm2"];
You are currently using the same key, which is why it overwrites the value.
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.