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
This commit is contained in:
Riccardo Cipolleschi
2024-10-04 05:53:39 -07:00
committed by Facebook GitHub Bot
parent 1c923ad8ab
commit fdee0ebbcb
+8 -2
View File
@@ -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)