0

これが私がこれまでに試したことです:

- (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];
     }
}

私の問題は、タイマーを無効にすると、ランループがまだ実行されていて、UI がフリーズすることです (アニメーションが機能しない、スクロールが機能しないなど)。どうすればこれを達成できますか?

前もって感謝します!

4

1 に答える 1

1

timer を作成しないでくださいapplicationWillResignActive。代わりに、現在の日付/時刻を に保存する必要があります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
    }
}
于 2012-12-14T21:20:23.750 に答える