fix: make React Native work without AppDelegate window property (#49748)

Summary:
This PR makes React Native not relying on the `window` property in AppDelegate. When running in SwiftUI lifecycle mode / SceneDelegate mode there is window property on AppDelegate. This PR fixes crashes that happen because RN asserts window property is there.

## Changelog:

[IOS] [FIXED] - make React Native work without AppDelegate window property

Pull Request resolved: https://github.com/facebook/react-native/pull/49748

Test Plan: CI Green

Reviewed By: javache

Differential Revision: D70389691

Pulled By: cipolleschi

fbshipit-source-id: fe39f123b47014ba91a080239ccd298192c92a6a
This commit is contained in:
Oskar Kwaśniewski
2025-03-03 03:47:46 -08:00
committed by Facebook GitHub Bot
parent 7ccb1e1fb0
commit ae7bbe06c9
3 changed files with 6 additions and 7 deletions
@@ -34,7 +34,7 @@
std::lock_guard<std::mutex> lock(_mutex);
if (!_isObserving) {
_isObserving = YES;
_currentSafeAreaInsets = [UIApplication sharedApplication].delegate.window.safeAreaInsets;
_currentSafeAreaInsets = RCTKeyWindow().safeAreaInsets;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(_interfaceFrameDidChange)
name:RCTUserInterfaceStyleDidChangeNotification
@@ -64,7 +64,7 @@
- (void)_interfaceFrameDidChange
{
std::lock_guard<std::mutex> lock(_mutex);
_currentSafeAreaInsets = [UIApplication sharedApplication].delegate.window.safeAreaInsets;
_currentSafeAreaInsets = RCTKeyWindow().safeAreaInsets;
}
@end
@@ -209,11 +209,10 @@ static NSDictionary *RCTExportedDimensions(CGFloat fontScale)
- (void)interfaceOrientationDidChange
{
#if TARGET_OS_IOS && !TARGET_OS_MACCATALYST
UIApplication *application = RCTSharedApplication();
UIInterfaceOrientation nextOrientation = RCTKeyWindow().windowScene.interfaceOrientation;
UIWindow *keyWindow = RCTKeyWindow();
UIInterfaceOrientation nextOrientation = keyWindow.windowScene.interfaceOrientation;
BOOL isRunningInFullScreen =
CGRectEqualToRect(application.delegate.window.frame, application.delegate.window.screen.bounds);
BOOL isRunningInFullScreen = CGRectEqualToRect(keyWindow.frame, keyWindow.screen.bounds);
// We are catching here two situations for multitasking view:
// a) The app is in Split View and the container gets resized -> !isRunningInFullScreen
// b) The app changes to/from fullscreen example: App runs in slide over mode and goes into fullscreen->
@@ -222,7 +222,7 @@
#if TARGET_OS_MACCATALYST
return 0;
#else
return RCTSharedApplication().delegate.window.safeAreaInsets.bottom;
return RCTKeyWindow().safeAreaInsets.bottom;
#endif
}