Skip to content

Commit 3afaff6

Browse files
okwasniewskipull[bot]
authored andcommitted
feat: refactor RCTKeyWindow to be more resilient and work in multi-window apps (facebook#42036)
Summary: This PRs refactors `RCTKeyWindow()` to be more resilient and work in multi-window apps. After my recent PR got merged (facebook#41935) it significantly reduced the number of calls to `RCTKeyWindow()` and now it's called only when necessary. So this PR makes this function a bit more resource intensive but it guarantees that we will find current scene's key window. This would also fix some brownfield scenarios where React Native is working in multi-window mode and in the future allow us to more easily adopt `UIWindowSceneDelegate` bypass-github-export-checks ## Changelog: [IOS] [CHANGED] - refactor `RCTKeyWindow` to be more resilient and work in multi-window apps Pull Request resolved: facebook#42036 Test Plan: Checkout RNTester example for Alerts and LoadingView. https://github.com/facebook/react-native/assets/52801365/8cf4d698-db6d-4a12-8d8d-7a5acf34858b Reviewed By: huntie Differential Revision: D52431720 Pulled By: cipolleschi fbshipit-source-id: 0d6ef1d46b2428c30c9f64dae66b95dbc69f0a3b
1 parent ce00673 commit 3afaff6

File tree

3 files changed

+17
-32
lines changed

3 files changed

+17
-32
lines changed

packages/react-native/React/Base/RCTUtils.m

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -562,12 +562,20 @@ BOOL RCTRunningInAppExtension(void)
562562
return nil;
563563
}
564564

565-
// TODO: replace with a more robust solution
566-
for (UIWindow *window in RCTSharedApplication().windows) {
567-
if (window.keyWindow) {
568-
return window;
565+
for (UIScene *scene in RCTSharedApplication().connectedScenes) {
566+
if (scene.activationState != UISceneActivationStateForegroundActive ||
567+
![scene isKindOfClass:[UIWindowScene class]]) {
568+
continue;
569+
}
570+
UIWindowScene *windowScene = (UIWindowScene *)scene;
571+
572+
for (UIWindow *window in windowScene.windows) {
573+
if (window.isKeyWindow) {
574+
return window;
575+
}
569576
}
570577
}
578+
571579
return nil;
572580
}
573581

packages/react-native/React/CoreModules/RCTAlertController.mm

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,7 @@ @implementation RCTAlertController
2020
- (UIWindow *)alertWindow
2121
{
2222
if (_alertWindow == nil) {
23-
_alertWindow = [self getUIWindowFromScene];
24-
25-
if (_alertWindow == nil) {
26-
UIWindow *keyWindow = RCTSharedApplication().keyWindow;
27-
if (keyWindow) {
28-
_alertWindow = [[UIWindow alloc] initWithFrame:keyWindow.bounds];
29-
} else {
30-
// keyWindow is nil, so we cannot create and initialize _alertWindow
31-
NSLog(@"Unable to create alert window: keyWindow is nil");
32-
}
33-
}
23+
_alertWindow = [[UIWindow alloc] initWithWindowScene:RCTKeyWindow().windowScene];
3424

3525
if (_alertWindow) {
3626
_alertWindow.rootViewController = [UIViewController new];
@@ -65,16 +55,4 @@ - (void)hide
6555
_alertWindow = nil;
6656
}
6757

68-
- (UIWindow *)getUIWindowFromScene
69-
{
70-
for (UIScene *scene in RCTSharedApplication().connectedScenes) {
71-
if (scene.activationState == UISceneActivationStateForegroundActive &&
72-
[scene isKindOfClass:[UIWindowScene class]]) {
73-
return [[UIWindow alloc] initWithWindowScene:(UIWindowScene *)scene];
74-
}
75-
}
76-
77-
return nil;
78-
}
79-
8058
@end

packages/react-native/React/CoreModules/RCTDevLoadingView.mm

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,13 @@ - (void)showMessage:(NSString *)message color:(UIColor *)color backgroundColor:(
113113

114114
dispatch_async(dispatch_get_main_queue(), ^{
115115
self->_showDate = [NSDate date];
116+
116117
if (!self->_window && !RCTRunningInTestEnvironment()) {
117-
UIWindow *window = RCTSharedApplication().keyWindow;
118+
UIWindow *window = RCTKeyWindow();
118119
CGFloat windowWidth = window.bounds.size.width;
119120

120-
self->_window = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, windowWidth, window.safeAreaInsets.top + 10)];
121+
self->_window = [[UIWindow alloc] initWithWindowScene:window.windowScene];
122+
self->_window.frame = CGRectMake(0, 0, windowWidth, window.safeAreaInsets.top + 10);
121123
self->_label = [[UILabel alloc] initWithFrame:CGRectMake(0, window.safeAreaInsets.top - 10, windowWidth, 20)];
122124
[self->_window addSubview:self->_label];
123125

@@ -134,9 +136,6 @@ - (void)showMessage:(NSString *)message color:(UIColor *)color backgroundColor:(
134136

135137
self->_window.backgroundColor = backgroundColor;
136138
self->_window.hidden = NO;
137-
138-
UIWindowScene *scene = (UIWindowScene *)RCTSharedApplication().connectedScenes.anyObject;
139-
self->_window.windowScene = scene;
140139
});
141140

142141
[self hideBannerAfter:15.0];

0 commit comments

Comments
 (0)