Last active
February 27, 2021 19:04
-
-
Save ian-mcdowell/1fda47126429df43cc989d02c1c5e4a0 to your computer and use it in GitHub Desktop.
UIDebuggingInformationOverlay for iOS 10, 11, and 12
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <objc/runtime.h> | |
@interface DebuggingOverlay: NSObject | |
@end | |
@implementation DebuggingOverlay | |
+ (void)toggleOverlay { | |
id debugInfoClass = NSClassFromString(@"UIDebuggingInformationOverlay"); | |
// In iOS 11, Apple added additional checks to disable this overlay unless the | |
// device is an internal device. To get around this, we swizzle out the | |
// -[UIDebuggingInformationOverlay init] method (which returns nil now if | |
// the device is non-internal), and we call: | |
// [[UIDebuggingInformationOverlayInvokeGestureHandler mainHandler] _handleActivationGesture:(UIGestureRecognizer *)] | |
// to show the window, since that now adds the debugging view controllers, and calls | |
// [overlay toggleVisibility] for us. | |
if (@available(iOS 11.0, *)) { | |
id handlerClass = NSClassFromString(@"UIDebuggingInformationOverlayInvokeGestureHandler"); | |
static dispatch_once_t onceToken; | |
dispatch_once(&onceToken, ^{ | |
// Swizzle init of debugInfo class to just call [UIWindow init] | |
Method initMethod = class_getInstanceMethod(debugInfoClass, @selector(init)); | |
IMP newInit = method_getImplementation(class_getInstanceMethod([UIWindow class], @selector(init))); | |
method_setImplementation(initMethod, newInit); | |
}); | |
id debugOverlayInstance = [debugInfoClass performSelector:NSSelectorFromString(@"overlay")]; | |
[debugOverlayInstance setFrame:[[UIScreen mainScreen] bounds]]; | |
UIGestureRecognizer *dummyGestureRecognizer = [[UIGestureRecognizer alloc] init]; | |
dummyGestureRecognizer.state = UIGestureRecognizerStateEnded; | |
id handler = [handlerClass performSelector:NSSelectorFromString(@"mainHandler")]; | |
[handler performSelector:NSSelectorFromString(@"_handleActivationGesture:") withObject:dummyGestureRecognizer]; | |
} else { | |
id debugOverlayInstance = [debugInfoClass performSelector:NSSelectorFromString(@"overlay")]; | |
[debugOverlayInstance performSelector:NSSelectorFromString(@"toggleVisibility")]; | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi Ian,
How to call this toggleOverlay method from swift class .