Skip to content

Instantly share code, notes, and snippets.

@zats
Created May 20, 2014 12:18
Show Gist options
  • Save zats/1a4aece697075478b44a to your computer and use it in GitHub Desktop.
Save zats/1a4aece697075478b44a to your computer and use it in GitHub Desktop.
Fix for ImagePicker's camera resetting [UIApplication sharedApplication].idleTimerDisabled = YES;
@interface ZTSCameraControllerHelper ()
@property (nonatomic, assign, getter = isIdleTimerDisabled) BOOL idleTimerDisabled;
@end
@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];
}
}
@end
@zats
Copy link
Author

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