You shouldn't create a timer applicationWillResignActive
. Instead you should save the current date/time in applicationDidEnterBackground
.
// Not sure how you are keeping session information
// You can use a variable to store session id
// or simple keep a bool to indicate session is valid
// In this example, let say I just keep a session BOOL
- (void)applicationDidEnterBackground:(UIApplication *)application {
// save the save the app enters background
backgroundTime_ = [NSDate date];
}
// In this example I am going to check if my session is valid in two stages
// You can do it in one stage if you like
- (void)applicationWillEnterForeground:(UIApplication *)application {
// I only need to do a time-out check if I have a valid session
if (isValidSession_ && backgroundTime_)
{
// get the number of second since we entered background
NSTimeInterval span = [backgroundTime_ timeIntervalSinceNow];
if (span > (15 * 60))
{
isValidSession_ = NO;
}
}
}
// This is wheer the magic occurs
- (void)applicationDidBecomeActive:(UIApplication *)application {
// check if session is still valid
if (!isValidSession_)
{
// Load the login view
}
}