From 503aa827a9c92cc54a29e05ccf8b7a52f6faeb5d Mon Sep 17 00:00:00 2001 From: Duy Tran Khanh Date: Tue, 18 Oct 2022 18:18:43 +0700 Subject: [PATCH] Workaround random UIViewControllerHierarchyInconsistency crashes --- Natives/UIKit+hook.m | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/Natives/UIKit+hook.m b/Natives/UIKit+hook.m index d87482ec..e21845e3 100644 --- a/Natives/UIKit+hook.m +++ b/Natives/UIKit+hook.m @@ -8,6 +8,7 @@ void swizzle(Class class, SEL originalAction, SEL swizzledAction) { __attribute__((constructor)) void hookConstructor(void) { swizzle(UIDevice.class, @selector(userInterfaceIdiom), @selector(hook_userInterfaceIdiom)); + swizzle(UIResponder.class, @selector(becomeFirstResponder), @selector(hook_becomeFirstResponder)); swizzle(UIToolbar.class, @selector(sizeThatFits:), @selector(hook_sizeThatFits:)); swizzle(UIView.class, @selector(didMoveToSuperview), @selector(hook_didMoveToSuperview)); swizzle(UIView.class, @selector(setFrame:), @selector(hook_setFrame:)); @@ -36,6 +37,24 @@ __attribute__((constructor)) void hookConstructor(void) { @end +@implementation UIResponder(hook) + +- (BOOL)hook_becomeFirstResponder { + // Workaround a weird bug: UIKit tries to present an accessibility UITextField + // for some reason, which could cause crash due to mismatched parent view controller + // UIViewControllerHierarchyInconsistency + + @try { + return [self hook_becomeFirstResponder]; + } @catch (NSException *e) { + NSLog(@"-[UITextInputUIResponderAccessibility becomeFirstResponder]: %@", e); + } + + return NO; +} + +@end + @implementation UITraitCollection(hook) - (UIUserInterfaceSizeClass)verticalSizeClass {