Last active
July 8, 2025 20:08
-
-
Save EthanArbuckle/a2bc0e45b5966d8e9d610b1a66bde9b1 to your computer and use it in GitHub Desktop.
drawing a UIWindow from an iOS cli tool
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 <CoreGraphics/CoreGraphics.h> | |
| #import <Foundation/Foundation.h> | |
| #import <objc/runtime.h> | |
| #import <objc/message.h> | |
| #import <dlfcn.h> | |
| __attribute__((constructor)) static void init(void) { | |
| Method bundleIdentifierMethod = class_getInstanceMethod(objc_getClass("NSBundle"), sel_registerName("bundleIdentifier")); | |
| IMP newImp = imp_implementationWithBlock(^(id self) { | |
| return @"com.foo"; | |
| }); | |
| method_setImplementation(bundleIdentifierMethod, newImp); | |
| } | |
| int main(int argc, char * argv[]) { | |
| @autoreleasepool { | |
| void *graphics_services = dlopen("/System/Library/PrivateFrameworks/GraphicsServices.framework/GraphicsServices", RTLD_LAZY); | |
| void *_GSInitialize = dlsym(graphics_services, "GSInitialize"); | |
| void *_GSEventInitialize = dlsym(graphics_services, "GSEventInitialize"); | |
| void *_GSEventPushRunLoopMode = dlsym(graphics_services, "GSEventPushRunLoopMode"); | |
| void *_GSSetMainScreenInfo = dlsym(graphics_services, "GSSetMainScreenInfo"); | |
| ((void (*)(void))_GSInitialize)(); | |
| void *bks = dlopen("/System/Library/PrivateFrameworks/BackBoardServices.framework/BackBoardServices", RTLD_LAZY); | |
| void *_BKSDisplayServicesStart = dlsym(bks, "BKSDisplayServicesStart"); | |
| ((void (*)(void))_BKSDisplayServicesStart)(); | |
| void *uikit = dlopen("/System/Library/Frameworks/UIKit.framework/UIKit", RTLD_LAZY); | |
| void *_UIApplicationInitialize = dlsym(uikit, "UIApplicationInitialize"); | |
| void *_UIApplicationInstantiateSingleton = dlsym(uikit, "UIApplicationInstantiateSingleton"); | |
| ((void (*)(void))_UIApplicationInitialize)(); | |
| id mainScreen = ((id (*)(id, SEL))objc_msgSend)(objc_getClass("UIScreen"), sel_registerName("mainScreen")); | |
| CGRect mainScreenBounds = ((CGRect (*)(id, SEL))objc_msgSend)(mainScreen, sel_registerName("bounds")); | |
| CGFloat mainScreenScale = ((CGFloat (*)(id, SEL))objc_msgSend)(mainScreen, sel_registerName("scale")); | |
| ((void (*)(CGSize, CGFloat, int))_GSSetMainScreenInfo)(mainScreenBounds.size, mainScreenScale, 0); | |
| Class mockAppClass = objc_allocateClassPair(objc_getClass("UIApplication"), "MockApp", 0); | |
| objc_registerClassPair(mockAppClass); | |
| class_addProtocol(mockAppClass, objc_getProtocol("UIApplicationDelegate")); | |
| ((void (*)(Class))_UIApplicationInstantiateSingleton)(mockAppClass); | |
| ((void (*)(BOOL))_GSEventInitialize)(NO); | |
| ((void (*)(CFRunLoopMode))_GSEventPushRunLoopMode)(kCFRunLoopDefaultMode); | |
| id UIApp = ((id (*)(id, SEL))objc_msgSend)(mockAppClass, sel_registerName("sharedApplication")); | |
| ((void (*)(id, SEL))objc_msgSend)(UIApp, sel_registerName("_accessibilityInit")); | |
| id mainCADisplay = ((id (*)(id, SEL))objc_msgSend)(mainScreen, sel_registerName("_display")); | |
| id displayConfiguration = [objc_getClass("FBSDisplayConfiguration") alloc]; | |
| displayConfiguration = ((id (*)(id, SEL, id, BOOL))objc_msgSend)(displayConfiguration, sel_registerName("initWithCADisplay:isMainDisplay:"), mainCADisplay, YES); | |
| id rootWindow = [objc_getClass("UIRootSceneWindow") alloc]; | |
| rootWindow = ((id (*)(id, SEL, id))objc_msgSend)(rootWindow, sel_registerName("initWithDisplayConfiguration:"), displayConfiguration); | |
| ((void (*)(id, SEL, CGRect))objc_msgSend)(rootWindow, sel_registerName("setFrame:"), CGRectMake(100, 100, 200, 200)); | |
| id blueColor = ((id (*)(id, SEL))objc_msgSend)(objc_getClass("UIColor"), sel_registerName("blueColor")); | |
| ((void (*)(id, SEL, id))objc_msgSend)(rootWindow, sel_registerName("setBackgroundColor:"), blueColor); | |
| ((void (*)(id, SEL, BOOL))objc_msgSend)(rootWindow, sel_registerName("setHidden:"), NO); | |
| ((void (*)(id, SEL, int))objc_msgSend)(rootWindow, sel_registerName("setWindowLevel:"), 9999); | |
| NSLog(@"presenting window: %@", rootWindow); | |
| CFRunLoopRun(); | |
| return 0; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I see. It's quite challenging to make it work, especially since handling HID events have changed iOS 15 and up. I don't really wanna deal with AccessibilityUtilities and manually fetch touch events, but I guess I have no choice...