From fdee0ebbcb88f40c23b92b07792fbb9d1041c546 Mon Sep 17 00:00:00 2001 From: Riccardo Cipolleschi Date: Fri, 4 Oct 2024 05:53:39 -0700 Subject: [PATCH] Avoid calling keyWindow on any UIScene (#46832) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/46832 After bumping to minIOSVersion 15.1, we refactored the code to remove some check. In the refactoring, we changed how the `keyWindow` is returned and now we are unsafely casting `UIScene` to `UIWindowScene`. We have some internal apps that use `UIScene` that are not `UIWindowScene` and the change is causing them to crash. This change fixes the crash by checking whether the selector is available in the UIScene and casting it only in that case. Otherwise we return `nil`, the same behavior we used to have before the refactor. ## Changelog [iOS][Fixed] - Cast the UIScene to UIWindowScene only if the scene respond to the selector Reviewed By: javache Differential Revision: D63890980 fbshipit-source-id: 3230e0075f06ed3f3d759b48f9c7bd13d8787b44 --- packages/react-native/React/Base/RCTUtils.m | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/packages/react-native/React/Base/RCTUtils.m b/packages/react-native/React/Base/RCTUtils.m index bd0108c1f3f..22b55692471 100644 --- a/packages/react-native/React/Base/RCTUtils.m +++ b/packages/react-native/React/Base/RCTUtils.m @@ -584,9 +584,15 @@ UIWindow *__nullable RCTKeyWindow(void) } UIScene *sceneToUse = foregroundActiveScene ? foregroundActiveScene : foregroundInactiveScene; - UIWindowScene *windowScene = (UIWindowScene *)sceneToUse; - return windowScene.keyWindow; + if ([sceneToUse respondsToSelector:@selector(keyWindow)]) { + // We have apps internally that might use UIScenes which are not window scenes. + // Calling keyWindow on a UIScene which is not a UIWindowScene can cause a crash + UIWindowScene *windowScene = (UIWindowScene *)sceneToUse; + return windowScene.keyWindow; + } + + return nil; } UIStatusBarManager *__nullable RCTUIStatusBarManager(void)