Pergunta

I have an iPad survey tool as an internal enterprise application. I prevent screen locking with setting [[UIApplication sharedApplication] setIdleTimerDisabled: YES]; at didFinishLaunchingWithOptions of the application delegate.

That works fine until I use the imagePicker to take an image. After that, the idleTimer is activated again. I have tried to disable it after the image was taken but that doesn't work.

Here I found the hint that setting the required device capabilities in the info.plist could help. But so far it didn't. I have just added all camera specific flags.

Any ideas?

Many thanks!

Marcus

Foi útil?

Solução

I was able to reset the UIApplication idleTimerDisabled like so:

- (void)resetIdleTimerDisabled
{
    [[UIApplication sharedApplication] setIdleTimerDisabled:YES];
}

#pragma mark - UIImagePickerControllerDelegate

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [self dismissViewControllerAnimated:YES completion:^{
        [self performSelector:@selector(resetIdleTimerDisabled) withObject:nil afterDelay:1.0];
    }];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [self dismissViewControllerAnimated:YES completion:^{
        [self performSelector:@selector(resetIdleTimerDisabled) withObject:nil afterDelay:1.0];
    }];
}

What I suspect is happening is that internally UIImagePickerController sets UIApplication.idleTimerDisabled to YES to keep the camera from sleeping. When finished (after the delegate methods are called and apparently even after the animation completion block is executed), the UIImagePickerController sets UIApplication.idleTimerDisabled back to NO. Instead, it only should do this if the value was previously NO.

I filed a bug report with Apple. See UIImageViewControllerBug sample project.

Outras dicas

Jamie's solution looks good! I'm just not a big fan of afterDelay: methods:)

The issue is in the PhotoLibrary framework: it disables the idleTimer prior to starting preview stream from camera and enables it again when tearing it down regardless of the previous value.

If you feel more adventures and want solution that works throughout the app, here is one involving swizzling: https://gist.github.com/zats/1a4aece697075478b44a

Tested for both cases when idleTimer disabled or enabled prior to showing image picker. My solution does not observe idleTimerDisabled while image picker is presented.

P.S. the same issue happens when using dictation feature (bug in UIDictationController) (@jamie-mcdaniel if you would be so kind to update your bug report)

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top