How to implement the connection and disconnect in an application


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?

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.