5

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

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

[[ UIApplication sharedApplication ] setIdleTimerDisabled: YES ]

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

谢谢你的帮助。

4

2 回答 2

11

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

于 2009-06-21T07:52:31.050 回答
6

这是我在我的应用程序中使用的代码。一点背景知识:我的应用程序有一个内置的网络服务器,因此用户可以通过 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];
}
于 2009-11-07T17:21:41.990 回答