Shaunak Shaunak - 1 year ago 98
iOS Question

How to create and manage session in iphone app?

I have an application which uses wcf web service and after successful login, i want to activate a session of 10 minutes. If user is idle or app is in background for 10 minutes then session should expire and app should ask for login details again.

I have used keychain for storing username and password and it works But i want to add the session so that user is redirected to main page if session is not expired and to the login page if session is expired...

I know this is repeated question but i had to write it since i am not getting proper answer from other questions. Please help..

Answer Source

use this Link this works me.

use NSUserDefault to create Session for ios.

i also created Session in ios with this Tutorial.


i have a Login Screen in first Page of My application

if Login is Successfull authenticate by server then i stored my Username & Password in NSUserDefault Like this way:

 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
     [defaults setObject:userNameText forKey:@"username"];               
     [defaults setObject:passWordText forKey:@"password"];
     [defaults synchronize];

Onclick of Login Button. & redirected to Dashboard.

In dashboard Logout Button is there if user click Logout Delete All Data Like this way.

 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults removeObjectForKey:@"username"];
    [defaults removeObjectForKey:@"password"];
    [defaults synchronize];

& On other condition when user close app The NSUSerDefault Are stored when u second time open app at that time Check wheather username & password stored in NSUserDefault During ViewDidAppear of Loginpage.

  NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    // check if user is alraidy Login
    if([defaults objectForKey:@"username"]!=nil  && ![[defaults objectForKey:@"username"] isEqualToString:@""]){
        // Redirected to Dashboard.

If sucessfull then Redirected to Dashboard.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download