Store user and global data for use later - iphone

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

Related

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

How to add the data from tableview to sql

I am making an application. On one of the tab I have used tableview on which I am adding the local notifications timing by using time picker. I am also using sql database in my app. Now I want the timing which I have added in the table view must be store in database so that I can retrieve it on another tab. How Can I do this implementation. Please If anyone know it. Give me some solutions.
Thanks alot.
If all you need is to receive the data in an other tab I think storing in mysql is a bit overkill.
What don't you use a shared object that will be used by both tables?
An other simple option will be to store the data in NSUserDefaults
//create an array of all the data in the table view.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:array forKey:#"table data"];
[defaults synchronize];
And in the next tab
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSArray *array = [defaults objectForKey#"table data"];
//pass the array to the table view.
But that really depends on how much data you store and do you need it to be saved when the app is closed.
But the best solution in my opinion will be to use core data.
GOOD Luck

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.

Does NSUserDefaults save objects if the application becomes inactive?

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.

Making an in-app settings page (iPhone)

I am making an app which needs a settings page. Which will be a different class, so how do I pass data from one class to the other? For example: There is a UISwitch on the settings page, and I need to see what option the user selected for another class.
Thanks.
On your settings page, use the following code to synchronize your settings -
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject: data forKey: #""]; // The forKey is the name of the setting you're going to sync
[defaults synchronize];
And to get the value of the setting in your other controllers, use the following -
NSString *settingValue = [[NSUserDefaults standardUserDefaults] objectForKey: #""]; // The objectForKey is the name of the setting you're getting the value for.
Hope this helps you!