Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delaying but not disabling iPhone auto-lock

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.

like image 888
Sean R Avatar asked Jun 21 '09 05:06

Sean R


People also ask

How do I extend the time on my iPhone before locking?

You can set how long it takes iPhone to automatically lock. Go to Settings > Display & Brightness > Auto-Lock, then choose a length of time.

Why can't I set my iPhone to never auto lock?

Go to Devices > Password Policy > iOS tab. Look for the Auto-Lock dropdown option and set it to "–" to disable auto-lock on the devices. Save the changes, and wait for a few seconds until the devices receive the new policy changes. On the iOS device, go to Settings > General > Auto-Lock.

How do I change the delay on my iPhone?

Go to Settings, Display & Brightness, Auto Lock and you can set the delay from 30 seconds all the way to Never.


2 Answers

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.

like image 90
Nathan de Vries Avatar answered Sep 25 '22 07:09

Nathan de Vries


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];
}
like image 39
wkw Avatar answered Sep 22 '22 07:09

wkw