Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Fix for ImagePicker's camera resetting [UIApplication sharedApplication].idleTimerDisabled = YES;
@interface ZTSCameraControllerHelper ()
@property (nonatomic, assign, getter = isIdleTimerDisabled) BOOL idleTimerDisabled;
@implementation ZTSCameraControllerHelper
+ (instancetype)sharedInstance {
static ZTSCameraControllerHelper *instance;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[ZTSCameraControllerHelper alloc] init];
return instance;
+ (void)load {
[self swizzleMethods];
+ (void)swizzleMethods {
// To make sure PhotoLibrary framework classes are registered
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker = nil;
Class cameraControllerClass = NSClassFromString(@"PLCameraController");
// method called just before we render camera preview for the first time
Method delayMethod = class_getInstanceMethod(self, @selector(zts_delayIdleTimerByTimeInterval:));
IMP delayIMP = method_getImplementation(delayMethod);
PSPDFReplaceMethod(cameraControllerClass, sel_getUid("_delayIdleTimerByTimeInterval:"), @selector(zts_delayIdleTimerByTimeInterval:), delayIMP);
// method called after camera preview stopped
Method resetIdleTimerMethod = class_getInstanceMethod(self, @selector(zts_resetIdleTimer));
IMP resetIdleTimerIMP = method_getImplementation(resetIdleTimerMethod);
PSPDFReplaceMethod(cameraControllerClass, sel_getUid("_resetIdleTimer"), @selector(zts_resetIdleTimer), resetIdleTimerIMP);
- (void)zts_delayIdleTimerByTimeInterval:(NSTimeInterval)delay {
[ZTSCameraControllerHelper sharedInstance].idleTimerDisabled = [UIApplication sharedApplication].isIdleTimerDisabled;
if (![ZTSCameraControllerHelper sharedInstance].isIdleTimerDisabled) {
[self zts_delayIdleTimerByTimeInterval:delay];
- (void)zts_resetIdleTimer {
if (![ZTSCameraControllerHelper sharedInstance].isIdleTimerDisabled) {
[self zts_resetIdleTimer];
Copy link

zats commented May 20, 2014

Fix for ImagePicker's camera resetting [UIApplication sharedApplication].idleTimerDisabled = YES;

The same issue happens when you're using dictation feature - it effectively resets the idleTimerDisabled to NO.

The faulty calls are -[UIDictationController setupForDictationStartForReason:] and -[UIDictationController cancelDictation]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment