9

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

Shaunak
  • 707
  • 1
  • 9
  • 29

2 Answers2

10

use this Link this works me.

use NSUserDefault to create Session for ios.

i also created Session in ios with this Tutorial.

EDIT:

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.

Dixit Patel
  • 3,190
  • 1
  • 24
  • 36
  • Can you specify how did you created session using NSUserDefault? – Shaunak Feb 05 '13 at 08:48
  • 3
    Well, this is just for the autologin and doesnot start or expire the session if the user stays idle for very long time.. – Shaunak Feb 05 '13 at 09:38
  • you should try your self use of NStimer its just hint – Dixit Patel Feb 05 '13 at 09:39
  • for more information Refer this Example https://github.com/tejaswiyvs/AutoLogoutForiOS – Dixit Patel Feb 05 '13 at 09:46
  • i guess i will start a timer when app goes to background and when it enters foreground i would recheck timer and go to login/dashboard appropriately.. – Shaunak Feb 05 '13 at 09:51
  • I know this answer is old but this is the absolute WORST way to handle sessions for iOS. It was a bad answer back in '13 and it still is now. `NSUserDefaults` stores info in cleartext, a terrible security idea. – barndog Oct 15 '15 at 00:58
  • 1
    What if you change password at server side through other device ? – Viraj Padsala Oct 27 '15 at 04:30
  • How to clear app data on click of Logout Button. ? I having 100+ viewController.swift file?? – Coder Jul 15 '16 at 06:08
  • You should never store raw password of user either on device or cloud, store its hash instead. – Muhammed Gül May 26 '20 at 15:42
0

You can use AFNetworking framework, it auto store your session, I had tested my APIs and it worked

sooon
  • 4,718
  • 8
  • 63
  • 116