How to make my app stay login even after power-off on ipad? - iphone

I created a web service application. I want my user to allways stay login after installition but i have no idea about how to do this. I save user informations in NSUserDefaults. Thank in Advance
EDIT I log into the server, user informations are firstly created on server and i sen Username&Password ect via a GET to the server. If this data sent in GET are the same with ones in server, the user will login into the application. Anything(content) of application comes from server after logging in.

NSString *savedUserName = [[NSUserDefaults standardUserDefaults] stringForKey:#"userName"];
NSString *savedUserPass = [[NSUserDefaults standardUserDefaults] stringForKey:#"userPass"];
if (savedUserName && savedUserPass){ // check for length as well
//don't show login, create user with these credentials
}
else{
[[NSUserDefaults standardUserDefaults] setObject:#"newUserName" forKey:#"userName"];
[[NSUserDefaults standardUserDefaults] setObject:#"newUserPass" forKey:#"userPass"];
//then show login for logging in as a new user
}
Don't forget to save nil or empty string when user logs out.

Related

storing username and password in session iphone

I want to store username and password in a session , if a user login successfully then he will be directed to the welcome screen where a logout button is exist.
now if user close the application without log out the account and restart the app the he must be directed to the welcome screen skipping the login screen. and if user restart the app after logout then he must enter the username and password to come on welcome screen. help me out. i am new to iphone.
you may store the username and password in User Defaults or in Keychain (if want to keep it secure ). So on loading the app if you find username and password stored in Userdefaults , you may directly login to welcome screen and if not stored then show the login screen...
see this is the same as you want help with iphone session login
you can store whole array in the Userdefault, UserDefault is a one type of session in the iPhone...
here you can store any value or array in the UserDefaul..
In AppDelegate.h file just declare variable...
NSUserDefaults *userDefaults;
here it not compulsory to global declare this userDefault, you can direct use the userdefault where you want
after...
In AppDelegate.m Fille in applicationDidFinishLonching: Method or anywhere which you want to store username and pssword
userDefaults = [NSUserDefaults standardUserDefaults];
[userDefault setObject:yourName forKey:#"UserName"];
[userDefault setObject:yourPassword forKey:#"Password"];
[userDefault synchronize];
after that when you want get data from this UserDefaults Use Bellow Code...
NSString *userName = [[NSUserDefaults standardUserDefaults] valueForKey:#"UserName"];
NSString *password = [[NSUserDefaults standardUserDefaults] valueForKey:#"Password"];
i hope this helpful to you...
:)

How to implement log in and Log out in an app

I am using XCode4 for development.
In my applicaion I need login to a website to get the information. then by using that I have to perform some task.
I am accessing the website by REST service call. After the ASIHttpRequest passed
I am setting the user name & password like this.
[request setuserName:#"usernameString"];
[request setpassword:#"Passwordstring"];
But I am not aware of whether any session created or not.
So at the time of log out I only redirect the controller to log in screen & made the userName and password field blank.
But after logout it is taking wrong user name and password to login again.
My question:
Whether I have to create a session while log in and time out the session at the time of log out. How to do that?
In iphone application how Log in & Log out generally done?
you best choice will be using NSUserDefaults class, example:
// saving your data.
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
[standardUserDefaults setObject:myUsername forKey:#"username"];
// retrieve your data
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
NSString *username = [standardUserDefaults objectForKey:#"username"];
you can also use NSUserDefaults to store the Log in status of the current session by for example storing a Boolean key for isLoggedIn key
[standardUserDefaults setBOOL:TRUE forKey:#"isLoggedIn"];
When the user wishes to log out just set the value to FALSE to offer the user the log in view again as if this is the first time the user is using the app.
You don't need to set timeout session for the logged in users, unless your design or the requirements said so, but you can also do that easily by using NSUserDefaults by storing the date and time of the user log in, and check that continuously to validate the session timeout, but I don't recommend to do such a thing, but everything is possible here.
Thanks all. This problem is solved.
At the time of log in, ASIHTTPRequest is creating a cookie object on successful log in. If a user did not spend 1 minute inside the application and came out by tapping sign out. After that it was taking any userId and password (garbage data) to login again. It happened because the previous cookie was taking care for that.
But, now at the time of sign out, I am deleting the cookie which is created at the time of log in.
I have added below code in sign out:-
NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (NSHTTPCookie *each in [[[cookieStorage cookiesForURL:YOUR_URL] copy] autorelease])
{
// NSLog(#"cookie deleted==%#",each);
[cookieStorage deleteCookie:each];
}
AhmadTK's answer is fine, just a couple of points:
Always make sure you call [standardUserDefaults synchronize]; after setting values.
Initiate a check of the isLoggedIn item when the application starts, and if false, present the login screen for the user
Handle the logout process in the reverse (set the isLoggedIn value to NO)

How to use NSUserDefaults for creating the auto login settings for application

I know this very regular question But i didn't get that's way ask here I hope people will me out .
I have application which have the login page .And when ever I login I get Token_ID from server .I get this TokenID with help of JSON.
But I want to add the Auto Login function to my application .For that My application look like this
It has the two text field (username and password)
one check box for auto login And login button
I need when ever user click on the check box which is for auto login It must store TokenID in application and when I close my application .And start the application again It must not ask for login me again.It start the same application with same TokenID which I store for that user not a new one .And if I click the log out button it must logout from application And release the TokenID from my application .And after that if start the application it must ask for login with out that it must not go further in application
For that I do R&D from two days .And I found that I must use the NSUserDefaults for storing the TokenID which I am getting at the time of login .I want know how can I store the TokenID
into application and How can check it out the user logout or not .And if user not logout it start the application.And if user logout than it must ask for login to start the application
OR is that any idea please give And explain me
Thank you
First result at google for "NSUSerDefaults Example"
On logout just overwrite the keys with nil and check on starting of the app if the key is nil
you can save username information
-(void)saveToUserDefaults:(NSString*)myString
{
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
if (standardUserDefaults) {
[standardUserDefaults setObject:myString forKey:#"username"];
[standardUserDefaults synchronize];
}
}
if you have username information on NSUserDefaults, you can get your username information
-(NSString*)retrieveFromUserDefaults
{
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
NSString *val = nil;
if (standardUserDefaults)
val = [standardUserDefaults objectForKey:#"username"];
return val;
}

YAFBLQ: How to pre-populate FB login and password fields?

Yet another fb login question:
By the time my user has something to share, they've already logged into my app server.
I give them the option to use their FB creds for my app.
So if they share, I want to pre-populate the FB login page with the creds they've already supplied to me.
Does anyone know if this is possible, and if so, how to do it?
Here's what I'm talking about:
I use Facebook API too. You never keep login and pass in your app (check Facebook API doc). However you can keep session. So the user enter login and pass once. In my code I've a "FacebookLogger" who is a singleton with a Facebook object.
To store session I use NSUserDefaults (find doc here).
[[NSUserDefaults standardUserDefaults] setObject:m_Facebook.accessToken forKey:#"AccessToken"];
[[NSUserDefaults standardUserDefaults] setObject:m_Facebook.expirationDate forKey:#"ExpirationDate"];
[[NSUserDefaults standardUserDefaults] synchronize];
Where m_Facebook is my Facebook object in my singleton. After that I can catch Access with :
m_Facebook.accessToken = [[NSUserDefaults standardUserDefaults] stringForKey:#"AccessToken"];
m_Facebook.expirationDate = (NSDate *) [[NSUserDefaults standardUserDefaults] objectForKey:#"ExpirationDate"];

Check if app has been purchased

I am a beginner; I am trying to solve this but am unable to do so.
I have created an app that displays a few options in a table. When the user taps the cell the details of selected option is shown on another page (details are stored in a plist file).
What I want to do is to set it up so that if the user has purchased the app then only the details should be visible, but if the user has not purchased it, the user should be prompted to do so.
I have created a product ID for iTunes Connect and also created a testing account to test the app.
My problem is with the code: how should i check if user has already made the purchase?
Using NSUserDefaults is the easiest solution.
After a successful purchase:
[[NSUserDefaults standardUserDefaults]
setObject:#"purchased"
forKey:#"myPaidItem"];
[[NSUserDefaults standardUserDefaults]
synchronize];
And when you need to check if the user has purchased the item:
NSString* isPurchased =
[[NSUserDefaults standardUserDefaults]
stringForKey:#"myPaidItem"];
if ([#"purchased"
compare:isPurchased]==NSOrderedSame) {
........ }
Hope that helps
you have to store this information in appContext as a bool if the transaction is OK , boo= true
and when he want to buy another time you hve to check the value of boo wich is stored in app Context