Skip to content

Instantly share code, notes, and snippets.

@ian-mcdowell
Last active February 27, 2021 19:04
Show Gist options
  • Star 28 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save ian-mcdowell/1fda47126429df43cc989d02c1c5e4a0 to your computer and use it in GitHub Desktop.
Save ian-mcdowell/1fda47126429df43cc989d02c1c5e4a0 to your computer and use it in GitHub Desktop.
UIDebuggingInformationOverlay for iOS 10, 11, and 12
#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
@mohammedshahid
Copy link

Hi Ian,

How to call this toggleOverlay method from swift class .

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