Delaying but not disabling iPhone auto-lock
Asked Answered
L

2

5

I currently have a very simple app for which the only interaction is shaking the iPhone. However eventually the screen dims and auto-locks since the iPhone is not getting any touch events. I was wondering if there is a way to reset the auto-lock time-out when shaken?

I know that to disable auto-lock completely I would do this:

[[ UIApplication sharedApplication ] setIdleTimerDisabled: YES ]

but I don't really want to disable it completely; if the iPhone is legitimately not being used it should auto-lock as expected.

Thanks for your help.

Landseer answered 21/6, 2009 at 5:18 Comment(1)
I thought baby-shake got banned.Threegaited
C
11

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.

Carminacarminative answered 21/6, 2009 at 7:52 Comment(1)
OK, I'll give that a go. I thought there might just be a resetIdleTimer method or something a bit cleaner.Landseer
K
6

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];
}
Kerstin answered 7/11, 2009 at 17:21 Comment(2)
This code is very useful, we dropped it straight into our app, thanks! Just FYI, my testing with an iPad 2 running iOS 5.0.1 seems to suggest that the fixed amount of time actually becomes the minimum idle time - ie. if the user's configured lock delay is less, the screen locks also immediately after the setDileTimerDisabled:NO call. If the users lock time is longer than the fixed amount of time, the above code has no effect.Atonal
My code is pre-iOS4 so, it's good you provided your observations on the newer versions of iOS.Kerstin

© 2022 - 2024 — McMap. All rights reserved.