延迟但不禁用iPhone自动锁定

Sea*_*n R 5 iphone timer delay python-idle

我目前有一个非常简单的应用程序,唯一的互动是摇动iPhone.但最终屏幕变暗和自动锁定,因为iPhone没有任何触摸事件.我想知道是否有办法在摇动时重置自动锁定超时?

我知道要完全禁用自动锁定,我会这样做:

[[ UIApplication sharedApplication ] setIdleTimerDisabled: YES ]
Run Code Online (Sandbox Code Playgroud)

但我真的不想完全禁用它; 如果合法地不使用iPhone,它应该按预期自动锁定.

谢谢你的帮助.

Nat*_*ies 11

您可以[UIApplication sharedApplication].idleTimerDisabled根据自己的NSTimer或行为手势(摇动手机)的值来切换值.它可以在您的应用程序中设置为YES/ NO多次.


wkw*_*wkw 6

这是我在我的应用程序中使用的代码.一点背景:我的应用程序有一个内置的Web服务器,因此用户可以通过WIFI从浏览器访问数据,每次请求到达服务器时,我都会延长锁定计时器(在这种情况下至少2分钟;一旦重新启用,您仍然可以获得默认的时间量.

// 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];
}
Run Code Online (Sandbox Code Playgroud)