To save username & password using objective c - iphone

I am having login view & after login some list is shown...so i want to save username & password so that login view does not appear once if login made with correct ID,password directly it will show the list

As others have suggested, you can use NSUserDefaults.
If you are storing the password, then I would suggest you also add a level of security to this. You could use Secure NSUserDefaults (I've not used this personally, but seen a few people reporting it being useful). You can find that here
Or you can use the KeyChain API

If you do not care about encryption, you could save these very simply with
[[NSUserDefaults standardUserDefaults] setValue:#"username" forKey:#"username"];
[[NSUserDefaults standardUserDefaults] setValue:#"secret" forKey:#"password"];
and check those during program startup with
if (![[NSUserDefaults standardUserDefaults] valueForKey:#"username"]) {
// show the login screen
}

you may use NSUserDefaults
Apple Documentation about NSUserDefaults

You can save this in the NSUserDefaults like this :
NSUserDefaults *userDefaults = [NSUserDefaults standardDefaults];
[userDefaults setBool#"YES" forlkey#"userDidLogin"];
And you test like this :
NSUserDefaults *userDefaults = [NSUserDefaults standardDefaults];
if([userDefaults boolForKey:#"userDidLogin"])
{
.... // Go to the list View
}
This is just an indication, if you would like to put it to work , you should show your code how you implement the implémentation of login interface

NSUserDefaults is just a handy way designed for customize app's preferences. In normal situation, an app will stay in it's own sandbox and therefore other apps won't be able to access it.
However it is not the best choice to do so. It's not good practice. What you really need is keychain and see here for apple's sample code for it, it's much safer in terms of security.

there are multiple ways to save user name and password..
You can save in UserDefalt as above answer.
also you can save in plist.

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)

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"];

iPhone / iOS Facebook SDK - can you log in within the app, and keep the login credentials?

Is it possible to use the Facebook iOS SDK to authenticate within an app (not go to Safari), and also keep those authentication credentials for the next launch of the app?
When I try to use the demo app in the simulator, it always goes to safari to authenticate, which seems a bit crappy. And then, when I authenticate... if I completely kill the app it will ask to authenticate again (and then tell me I am already logged in)
Is there a way, to just present the user with just an email field and a password field and then keep that info.. within the app?
Take a look at this question and answer: Iphone facebook connect example calls safari. I don't want to use safari. Also, you'll want to store the authentication stuff in NSUserDefaults and check for them to make to prevent re-logins.
EDIT Some sample code:
To save login stuff:
[[NSUserDefaults standardUserDefaults] setObject:_facebook.accessToken forKey:#"AccessToken"];
[[NSUserDefaults standardUserDefaults] setObject:_facebook.expirationDate forKey:#"ExpirationDate"];
[[NSUserDefaults standardUserDefaults] synchronize];
To check for login stuff:
_facebook = [[[Facebook alloc] initWithAppId:#"[app_id]"] retain];
_facebook.accessToken = [[NSUserDefaults standardUserDefaults] stringForKey:#"AccessToken"];
_facebook.expirationDate = (NSDate *) [[NSUserDefaults standardUserDefaults] objectForKey:#"ExpirationDate"];
if (![_facebook isSessionValid]) {
[_facebook authorize:_permissions delegate:self];
}
else {
[_facebook requestWithGraphPath:#"me" andDelegate:self];
}
You can hack round it to stop it if this is what you really want, bearing in mind most other apps that migrate from the older Facebook connect api to graph will behave in the new way
In facebook.m find the following method
- (void)authorizeWithFBAppAuth:(BOOL)tryFBAppAuth
safariAuth:(BOOL)trySafariAuth
find the bottom of the didOpenOtherApp logic and comment out all above it so that it always opens inline and tuns this section of code thats contained in the !didOpenOtherApp braces
// If single sign-on failed, open an inline login dialog. This will require the user to
// enter his or her credentials.
if (!didOpenOtherApp) {
[_loginDialog release];
_loginDialog = [[FBLoginDialog alloc] initWithURL:loginDialogURL
loginParams:params
delegate:self];
[_loginDialog show];
}
However by doing this you are making it more likely that the user will have to input their credentials, which is surely worse than putting up with the fast app switching approach?
When you first auth, make sure you're asking for "offline_access" permission. That will make the token that OAuth returns to you NOT be invalidated at the end of the session, but instead stay valid literally until they come along and use the API to log your app OUT of Facebook.
Then, obviously, you need to save the token (I feel like NSUserDefaults is a fine place for it) and reuse it on later FB interactions.

logout from Twitter in iPhone using OAuth+MGTwitterEngine Library

I had made the twitter application using the OAuth and MGTwitterEngine Library. The login phase is working fine but I am facing problem in logout. I had referred all the post of logout for OAuth but it doesn't works. So can anyone suggest the perfect method for logout from the Twitter ...? OR What changes I have to make in the Library file for the logout..!!
Did anyone ever find the solution for this? If so, please post!
UPDATE: I figured it out. In the MGTwitterEngine.m, search for _clearCookies in initWithDelegate method. This is currently set to NO, change it to YES. In your action method for logging out the current user, call [_engine clearAccessToken]. Now run it, and voila, it works!
There is no sign out from Twitter OAuth/xAuth... you need to implement client side solution:
persistently store the access token in the keychain or coredata (it never expires unless the user revoke your application from his/her account) when log in and use it in subsequent calls to twitter and " keeping the user signed in"
when signing out delete any related data stored (in this case the access token)
hope that will help
I dont know how to logout. But if you want the login screen every time, do one thing (i dont know this is the right way), delete the login specific data which the engine saves in Userdefaults.
I don't see it explicitly stated here, but I used the "clearAccessToken" function in the SA_OAuthTwitterEngine to "log out" the current user.
Might be kinds of stupid, but this seems to work
- (IBAction)logout:(id)sender {
[_engine dealloc];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults removeObjectForKey:#"authData"];
[defaults synchronize];
[self presentLoginView];
}
I use this tutorial for integreate twitter to iOS app link
For logout I use next:
[_engine clearAccessToken];
_engine - this is instance of SA_OAuthTwitterEngine class.
You can call this method on your IBAction method.
Also need this method
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults removeObjectForKey:#"authData"];
[defaults synchronize];
You must delete your account in Twitter application which is in the Settings menu of your iPhone.