Delaying but not disabling iPhone auto-lock

核能气质少年 提交于 2019-11-30 07:35:46

You could toggle the value of [UIApplication sharedApplication].idleTimerDisabled based on the value of your own NSTimer or behavioral gesture (shaking the phone). It can be set to YES/NO multiple times in your application.

Here's the code I use in my app. A bit of background: my app has a built-in web server so users can access data from a browser over WIFI and each time a request arrives in the server, I extend the lock timer (for a minimum of 2 minutes in this case; you still get the default amount of time added on once re-enabled).

// disable idle timer for a fixed amount of time.
- (void) extendIdleTimerTimeout
{
    // cancel previous scheduled messages to turn idle timer back on
    [NSObject cancelPreviousPerformRequestsWithTarget:self
        selector:@selector(reenableIdleTimer)
        object:nil];
    // disable idle timer
    [[UIApplication sharedApplication] setIdleTimerDisabled:YES];

    // re-enable the timer on after specified delay.
    [self performSelector:@selector(reenableIdleTimer) withObject:nil afterDelay: 60 * 2];

}

- (void) reenableIdleTimer
{
sharedApplication].idleTimerDisabled );
    [NSObject cancelPreviousPerformRequestsWithTarget:self
        selector:@selector(reenableIdleTimer)
        object:nil];
    // disable idle timer
    [[UIApplication sharedApplication] setIdleTimerDisabled:NO];
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!