Create login page only once - iphone

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.

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

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!

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.