Pregunta

Here is what I have tried so far:

- (void)applicationWillResignActive:(UIApplication *)application
{

     timer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(triggerTimer:) userInfo:nil repeats:FALSE];
     NSRunLoop* runLoop = [NSRunLoop currentRunLoop];
     [runLoop addTimer:timer forMode:NSRunLoopCommonModes];
     [runLoop run];
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    if (timer && [timer isValid]) {
        [timer invalidate];
     }
}

My problem is that if I invalidate the timer the runloop is still running and freezing my UI (animations not working, scrolling not working, etc). Any ideas how could i accomplish this?

Thanks in advance!

¿Fue útil?

Solución

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
    }
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top