Search code examples
iosidle-timer

IOS: How do you prevent the screen from greying out immediately when re-enabling the idleTimer


I'm working on an iOS game that primarily uses accelerometer input. Previous programmers set idleTimerDisabled=YES on startup and left it that way. I recently made a change such that the idle timer is only disabled during gameplay, and is re-enabled when a level ends.

The problem is, if the play time of the level is longer than the user's idleTimer setting, the screen will grey out the moment I set idleTimerDisabled=NO. Is there a way to reset the timer upon re-enabling so that the full time increment will occur before the idleTimer dims the screen?


Solution

  • Disable the idle timer on a timer, using something like this.

    [self performSelector:@selector(enableIdleTimer) withObject:nil afterDelay:4];
    
    - (void)enableIdleTimer { 
    
        [[UIApplication sharedApplication] setIdleTimerDisabled:NO];
    }
    

    Alternately, this is a more modern and simple approach:

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [[UIApplication sharedApplication] setIdleTimerDisabled:NO];
    });