umakanta
umakanta

Reputation: 1059

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:

  1. 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?

  2. In iphone application how Log in & Log out generally done?

Upvotes: 1

Views: 1654

Answers (3)

umakanta
umakanta

Reputation: 1059

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

Upvotes: 2

Ahmad Kayyali
Ahmad Kayyali

Reputation: 8243

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.

Upvotes: 2

LJ Wilson
LJ Wilson

Reputation: 14427

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)

Upvotes: 1

Related Questions