In my Iphone App, User logins with Username and password. At after user logged in, He can access every attribute, features through APIs. But i want to maintain a session. That is, If user doi开发者_JAVA百科ng nothing in app, After getting 30 mins, Session should expire and user must be asked to login with his Username and password.
I am currently working on something exactly the same.
I do the following scenario for the session validity:
By the first incoming request if the
Session Token
appeared to be invalid I try to re-generate the session key again silently in the background.If procedure #1 did not work for some reasons such as the user changed his password from somewhere else I redirect the user to the log in page again and I alert something like "Your session key has been expired please log in again".
To get more into this I store the Session key in the NSUserDefaults
:
// Saving the Key
[[NSUserDefaults standardUserDefaults] setValue:@"ABCDEFGHIJK" forKey:@"SessionKey"];
// Reading the Key
[[NSUserDefaults standardUserDefaults] stringForKey:@"SessionKey"];
Hope this helps.
Set previous time mPreviousTime = [NSDate timeIntervalSinceReferenceDate];
on login and appropriate kThresholdHideValue(In seconds) in your case it is 30min(30*60) and you can check whether the time elapsed using below method...
NSTimeInterval timeNow = [NSDate timeIntervalSinceReferenceDate];
if( ( mPreviousTime + kThresholdHideValue <= timeNow ) )
{
//Log out
}
And also call mPreviousTime = [NSDate timeIntervalSinceReferenceDate];
when user performs some action. so you can log out only if user does not perform any action for 30min.
精彩评论