Skip to content

Commit

Permalink
Avoid calling keyWindow on any UIScene (facebook#46832)
Browse files Browse the repository at this point in the history
Summary:

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
  • Loading branch information
cipolleschi authored and facebook-github-bot committed Oct 4, 2024
1 parent a1ec345 commit c501ed3
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions packages/react-native/React/Base/RCTUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -584,9 +584,15 @@ BOOL RCTRunningInAppExtension(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)
Expand Down

0 comments on commit c501ed3

Please sign in to comment.