how to disable re-login in iphone - iphone

I have developed an app which is related with signing in to an account and people can also get registered in my app, since those user info will be stored in a SQLite database.
The problem is that, when the user logs out of the account and then re-logs in, the app asks the user to login again. I dont want this to happen, as it should remember the user name and password and should login automatically.
Does anyone know how to get this done?

In your AppDelegate.m file , add the NSUserDefaults method ...
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
if ([standardUserDefaults valueForKey:#"dbID"]==nil) {
[standardUserDefaults setObject:#"1" forKey:#"dbID"];
[standardUserDefaults synchronize];
}
}
and also you have sane the username and password save the NSUserDefaults.
and for log out functionality , add the code logout code where you want the logout the applications ...
-(IBAction)Logout :(id)sender {
[[NSUserDefaults standardUserDefaults] setObject:#"" forKey:#"dbID"];
[NSUserDefaults resetStandardUserDefaults];
}
in the login time check the dbID is null then user login with the username and password. if the dbID is not null the login the old username and password.
if u are exit the without logout the applications , then store the dbID is "1" in the NSUserDefaults and redirect the view controller and For save the user name and password in the your logon time...for save
[[NSUserDefaults standardUserDefaults] setValue:#"Your text field value" forKey:#"Username"];
[[NSUserDefaults standardUserDefaults] setValue:#"Your text field value" forKey:#"password"];
read the
NSString * _UserName = [[NSUserDefaults standardUserDefaults] stringForKey:#"Username"];
NSString * _password = [[NSUserDefaults standardUserDefaults] stringForKey:#"password"];
username and password save the NSUserDefaults is indicated the u have store the username and password in NSUserDefaults.
if the you have logout the application by button , the make the NUll username and password save the NSUserDefaults .. like
[[NSUserDefaults standardUserDefaults] setValue:#"" forKey:#"Username"];
[[NSUserDefaults standardUserDefaults] setValue:#"" forKey:#"password"];

Use NSUserDefaults. This one will work fine
NSUserDefaults *rememberDefault = [NSUserDefaults standardUserDefaults];
if ([[rememberDefault valueForKey:#"Autologin"] isEqualToString:#"Autologin_on"]) {
btnAutoLogin.selected =NO;
}else {
btnAutoLogin.selected =YES;
}
In your Button Action Method :
-(IBAction)btnOnOffSwitch:(UIButton *)sender{
if (sender.selected) {
sender.selected = NO;
}else {
sender.selected = YES;
}
}
and when you want to check use this:
NSUserDefaults *rememberDefault = [NSUserDefaults standardUserDefaults];
if([[rememberDefault valueForKey:#"Autologin"] isEqualToString:#"Autologin_on"])
{
write your code here. to push directly to your home screen. this should be checked in your login screen
}

may b this will help you.
for your condition when user successfully login first time at that time make 1 dictionary for storing username , password and user id else make 3 different nsuserdefault object for this 3 types.
and when user comes again in that login screen check that nsuser default value is empty or has some value so if the value is there than it has already logsin and u can directly redirect him to next page .. else display the login page..
here i have write only the logic not source code so u can try it out your self and learn more :)..
hope this will help you.

Related

How can I create session?

I need to session. I couldn't find any thing about this issue. Therefore I used appdelegate global variable, but app global variable not save value long time.
I mean, my aim is login page. I don't want user to sign in many time in my app. How can I realize this approach?
save the data in NSUserDefaults,
NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
[defaults setObject:username forKey:#"username"];
[defaults synchronize];
NSString *username=[defaults objectForKey:#"username"] ;
You can use NSUserDefaults for this purpose.
Here's an example for you. Good luck!
- (void) createSession:(NSObject *anyObject) {
//write an object to NSUserDefaults with the key of "session"
[[NSUserDefaults standardUserDefaults] setObject:anyObject forKey:#"session"];
//persist the value in the store
[[NSUserDefaults standardUserDefaults] synchronize];
}
- (NSObject*) getSession {
//get an object from NSUserDefaults with the key of "session"
return [[NSUserDefaults] standardUserDefaults] objectForKey:#"session"];
}
- (void) exampleUsage {
NSObject* mySessionObject = [self getSession];
if (mySessionObject != nil) {
//i have a session
} else {
//i do NOT have a session
}
}
You could check with NSUserDefaults
NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
if(![defaults boolForKey:#"login"])
{
/// view to login or signup what ever
}
else
{
/// user already logged in
}
You must use NSUserDefault To Login Window.
NSUserDefaults def=[NSUserDefaults standardUserDefaults];
[def setObject:delegate.txtfield1.text forKey:#"UserName"];
[def setObject:delegate.txtfield2.text forKey:#"Password"];
[def synchronize];
If you want to retrive the NSUSERDefault data used this code.
NSString *UserName=[defaults objectForKey:#"UserName"] ;
NSString *Password=[defaults objectForKey:#"Password"] ;
And For Checking the status
if(![def boolForKey:#"UserName"])
{
}
else
{
}
try this one might be helpful to you......

NSUserDefaults, credentials removal on uncheck iPhone

I am using below code for for saving the username password, credentials saved properly but the issue is when I again run the application, it shows the credentials in textfields but checkbox remains empty (i am using images to show the box checked and unchecked).
My questions are:
1. How can i save the state of checkbox if it is checked or unchecked and
2. If it is unchecked how can i remove the credentials from nsuserdefaults.
- (IBAction)checkboxButton:(id)sender{
if (checkboxSelected == 0){
[checkboxButton setSelected:YES];
NSString *user = [userNameField text];
NSString *passwd = [passwordField text];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:user forKey:#"Username"];
[defaults setObject:passwd forKey:#"Password"];
[defaults synchronize];
NSLog(#"Data Saved");
checkboxSelected = 1;
} else {
[checkboxButton setSelected:NO];
checkboxSelected = 0;
//Saving Username Password in file
}
}
thank you ... :)
Well you can save the state of the checkbox in the NSUserDefaults.
To read do the following.
BOOL isChecked = [[NSUserDefaults standardUserDefaults] boolForKey:#"loginCheckBox"];
It will return NO (false) if it is not set.
To set the state:
[[NSUserDefaults standardUserDefaults] setBool:checkboxButton.selected forKey:#"loginCheckBox"];
To remove the values from the NSUserDefaults just set the properties to nil:
[defaults setObject:nil forKey:#"Username"];
[defaults setObject:nil forKey:#"Password"];
Also I can strongly advice you to store the password in the keychain.
since all the data saved in NSUserDefaults is stored plain text.
You can use the easy to SSKeyChain for accessing keychain api more easily.
for saving checkmark state you have to maintain a variable which has to be saved and read again to set the last state of checkmark and for removing values from NSUserDefaults : you can use:
removeObjectForKey method

Iphone App Registration User Data Log

At the start of my application i make my users to register first then take them to login View.I want to save the successfully registered users and let them get to the loginView instead of Register View.
How can i do this.As far i know iphone is registered as one user.Tell me what is the best way.And how to keep the user data and redirect to login view for a user?
Thanks
You can use NSUserDefaults to save user data such as login info.
Take a look at this tutorial and the reference
In Short, you would do something like:
Saving after registration:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setObject:#"UserName" forKey:#"UserName"];
[prefs setObject:#"Pass" forKey:#"Pass"];
// This is suggested to synch prefs, but is not needed
[prefs synchronize];
Retrieving on app start:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *userName = [prefs stringForKey:#"UserName"];
NSString *pass = [prefs stringForKey:#"Pass"];

cache username and password in webview (iphone)

I want to introduce a cache policy in my app.
Basically I have made a web view in which I have the functionality that when I open up that I'll have to login ,
I want to store that data in the cache now ..
like Facebook app .. when I delete that app from the background and open it again .. i dont have to add all the detail again.
how to do this ?
Did you try NSUserDefaults
NSString *value = #"MyUsername#domain";
NSUserDefaults *userPreferences = [NSUserDefaults standardUserDefaults];
[userPreferences setObject:value forKey:#"username"];
To fetch from UserDefaults -
NSString *_value= [[NSUserDefaults standardUserDefaults] stringForKey:#"username"];

Problem in NSUserDefaults

I am creating app for jobsite. It will display login page first. when we enter username and password webservice will return userid as response. with this user id we can get jobs.
I want to put jobs screen first and check if user id is null then open loginview. if it is not null then load jobs in tableview. I used Nsuserdefaults to save response when user first enter username password. Every thing is working fine. but userdefault key is showing null in console but my condition is loading output also.. this is strange,.
my code is
-(void)CheckCond{
NSUserDefaults* storeData = [NSUserDefaults standardUserDefaults];
NSLog(#"Value for userdefault %#",[storeData objectForKey:#"userId"]);
NSString *responseValue = [storeData objectForKey:#"userId"];
NSLog(#"first REsponse value :%#",responseValue);
if ([responseValue isEqualToString:#""]) {
[self LoginLogout];
}
else
{
NSLog(#"self.userId %#",self.userId);
NSUserDefaults* storeData = [NSUserDefaults standardUserDefaults];
[storeData setObject:self.userId forKey:#"userId"];
NSLog(#"%#",[storeData objectForKey:#"userId"]);
[self loadOutput];
}
}
Console output for first time login is
2011-09-09 13:21:29.438 ITClassifiedUniversal[2718:207] first REsponse
value :(null) 2011-09-09 13:21:29.438 ITClassifiedUniversal[2718:207]
self.userId 77533 2011-09-09 13:21:29.438
ITClassifiedUniversal[2718:207] 77533
After quitting app and restarting console output is :
2011-09-09 13:21:42.044 ITClassifiedUniversal[2731:207] Value for
userdefault (null) 2011-09-09 13:21:42.045
ITClassifiedUniversal[2731:207] first REsponse value :(null)
2011-09-09 13:21:42.045 ITClassifiedUniversal[2731:207] self.userId
(null) 2011-09-09 13:21:42.045 ITClassifiedUniversal[2731:207] (null)
AS above output shows NSUserdefault for userid is null?? whts the problem ?
This user id is coming from login view
// Create and set up job view controller.
jobView = [[JobViewController alloc] initWithNibName:#"JobView" bundle:nil];
jobView.userId = self.response;
// Create a nav controller with job view as its root controller.
UINavigationController *jobViewNavController = [[UINavigationController alloc] initWithRootViewController:jobView];
// Present the job view nav controller.
[self presentModalViewController:jobViewNavController animated:YES];
[jobView release];
[jobViewNavController release];
at last add synchronize statement then its give proper value
NSUserDefaults* storeData = [NSUserDefaults standardUserDefaults];
if ([storeData objectForKey:#"userId"]==nil)
{
[storeData setObject:self.userId forKey:#"userId"];
[storeData synchronize];
}
After setting your userId, you need to save the defaults:
[storeData synchronize];
So that they can be picked up next time.
Also, if you object is nil, then it won't return YES to isEqualToString. Your if statement should be
if (!responseValue || [responseValue isEqualToString:#""])
You can used below code. it may give what you need.
[[NSUserDefaults standardUserDefaults] setValue:userID forKey:#"userID"];
[[NSUserDefaults standardUserDefaults] synchronize];
Look at the AppDelegate.m of this sample code from Apple and you will know what was wrong with your code.
You have not called registerDefaults with the corresponding objects and keys for your NSUserDefaults.