Does NSUserDefaults save objects if the application becomes inactive? - iphone

For example:
NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
[user setObject:textField1.text forKey:#"receive_text1"];
[user setObject:textField2.text forKey:#"receive_text2"];
[user synchronize];
If I leave this app, just like from background to foreground, or relaunching the app
Could objects in NSUserDefaults still get the data that I set before?

NSUserDefault is persistent. So the data will be saved until you remove them or delete the app from the device.

Related

NSUserDefault doesn't work on real device(iPhone)

In my App, I am trying to implement SettingView by NSUserDefault. So, I wrote program code as follows:
Method example of saving user setting:
+ (void)setUserAutoPlaySetting:(BOOL)setting {
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setBool:setting forKey:kAutoPlaySettingKey];
[userDefaults synchronize];
}
This method is received user setting as BOOL and saves by NSUserDefault.
I confirmed my App save user setting on simulator but not on real Device.
In addition, this method is enable to save NO but disable to save YES on a real device.
Can anyone please tell how can I overcome this problem? Thanks.

Store user and global data for use later

Question: How and where should I store my users information and other global data that comes from the database when the user logs in?
Background: When my user logs into the application I want to do another database call (beyond the authentication process) that grabs a lot of the general global information out of the database and store it somewhere that my application can get it later during different parts of the application.
I am a new iPhone developer so I am always open to other recommendations and feedback.
You can store it in "AppDelegate.h" like declare a array or dict. there and when you got
data assign him.
The Another way is store all your data in NSUserDefault
//Save
NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
[userDefault setValue:YourData forKey:#"LoginData"];
[userDefault synchronize];
YourData should be in array or dictnory.
//Get
Array/dict = [[NSUserDefaults standardUserDefaults] valueForKey:#"LoginData"];
:)
You can use
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setValue:#"Value" forKey:#"Key"];
[userDefaults synchronize];
Then Use it,
NSString *st = [userDefaults valueForKey:#"Key"];
Hopefully this will help you

How to save variables after application shut down?

I want to save some integers after application shut down and restore them after application opening, what is the easiest way to do this?
You should store and load data from NSUserDefaults:
http://developer.apple.com/library/IOS/#documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/Reference/Reference.html
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
// to store
[defaults setObject:[NSNumber numberWithInt:12345] forKey:#"myKey"];
[defaults synchronize];
// to load
NSNumber *aNumber = [defaults objectForKey:#"myKey"];
NSInteger anInt = [aNumber intValue];
Check out the NSUserDefaults documentation. You can set arbitrary key-value pairs there which (as long as you call the shared user defaults object’s -synchronize at some point before your app terminates) will persist between launches.
You can save them in the NSUserDefaults. This is mainly used for preferences.
[[NSUserDefaults standardUserDefaults] setObject:someInteger forKey:#"someIntegerKey"];
[[NSUserDefaults standardUserDefaults] synchronize];
You can also save them to a Property List file if you have more data you'd like to store.
NSDictionary *someDictionary = [NSDictionary dictionaryWithObjectsAndKeys:someInt1, #"someIntKey1", someInt2, #"someIntKey2", nil];
[someDictionary writeToFile:somePath error:&error];
To save upon exiting the app place any code in
- (void)applicationWillTerminate:(UIApplication *)application
Look into using NSUserDefaults. This works like a dictionary that you can add key/value pairs to. You save the variables in your app delegate's applicationWillTerminate and applicationDidEnterBackground methods. You load the variables again in applicationDidFinishLoading.
The easiest way is to use NSUserDefaults. Your app delegate will get an -applicationWillTerminate: message when the app is about to shut down, and you can write your data to NSUserDefaults (or write it into your own file if the amount of data is large). Then, when your app starts up again, your app delegate will get an -applicationDidFinishLaunching, and you can read your data back again.
Serialize them and store them on memory. You have to do this before shut down and load when app is reopened

Create login page only once

Greetings,
I am a new programmer in iphone development . i am making an application with Open Erp i require to a login page which occur only the first time the app is run and also a settings page to change it when required.
I have used NSUSerDefaults to access variables around classes but not sure how to save it and log in automatically the second time the app is run.
I appreciate any help available .
Thank you
You can set object for key in NSUserDefaults as
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:username forKey:#"userName"];
and to get from defaults use -
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSString *user_name = [userDefaults objectForKey:#"userName"];
when you launch your app check for userdefauls for pre saved username if it is nil then display login view otherwise display view that you want.

Why is NSUserDefaults not saving my values?

Hi I am trying to use NSUserDefaults to save some default values in database. I am able to save the values in the NSUserDefaults (even checked it in NSLog).
Now I need the values in app delegate when the application is restarted. But I am not getting anything in the NSUserDefaults. Following is my code from my class where I save the values in NSUserDefaults:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setObject:appDel.dictProfile forKey:#"dict"];
NSLog(#"%#",[prefs valueForKey:#"dict"]);
Following is my code from App Delegagte:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSLog(#"%#",[prefs valueForKey:#"dict"]);
the above code always returns me null.
Can some one please help me?
If you terminate your app by pressing the home button (in the Simulator or on the device), your NSUserDefaults will get saved.
If you terminate your app by pressing "Stop" in Xcode (in the Simulator or on the device), your NSUserDefaults might get saved, but there's a good chance they won't. NSUserDefaults persists any changes periodically, and if you terminate the process before they've been persisted, they'll be gone. You can force the save by calling:
[[NSUserDefaults standardUserDefaults] synchronize];
Addendum:
In iOS4 (this answer was originally written when iOS3 was the public release), your NSUserDefaults may not get saved when pressing the home button. Manually calling [[NSUserDefaults standardUserDefaults] synchronize] in applicationDidEnterBackground: should ensure that your NSUserDefaults are saved correctly (this should really be a built-in behaviour IMO).
This code works fine for me .
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
if (standardUserDefaults) {
[standardUserDefaults setObject:myString forKey:#"Prefs"];
[standardUserDefaults synchronize];
}
You didn't say whether you are running on a device or in the simulator, but if you restart the application in the simulator, all preferences will be reset between launches if you launch from Xcode. The preferences will only be preserved if you relaunch from the simulator itself.
In my case I was saving and retrieving a string. When I synchronized after saving and then retrived in another thread, it was not working properly. The problem was solved by synchronizing both after saving and before retreiving.