Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions packages/react-native/React/Base/RCTUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -547,13 +547,20 @@ BOOL RCTRunningInAppExtension(void)
if (RCTRunningInAppExtension()) {
return nil;
}

for (UIScene* scene in RCTSharedApplication().connectedScenes) {
if (scene.activationState != UISceneActivationStateForegroundActive || ![scene isKindOfClass:[UIWindowScene class]]) {
continue;
}
UIWindowScene *windowScene = (UIWindowScene *)scene;

// TODO: replace with a more robust solution
for (UIWindow *window in RCTSharedApplication().windows) {
if (window.keyWindow) {
return window;
for (UIWindow *window in windowScene.windows) {
if (window.isKeyWindow) {
return window;
}
}
}

return nil;
}

Expand Down
24 changes: 1 addition & 23 deletions packages/react-native/React/CoreModules/RCTAlertController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,7 @@ @implementation RCTAlertController
- (UIWindow *)alertWindow
{
if (_alertWindow == nil) {
_alertWindow = [self getUIWindowFromScene];

if (_alertWindow == nil) {
UIWindow *keyWindow = RCTSharedApplication().keyWindow;
if (keyWindow) {
_alertWindow = [[UIWindow alloc] initWithFrame:keyWindow.bounds];
} else {
// keyWindow is nil, so we cannot create and initialize _alertWindow
NSLog(@"Unable to create alert window: keyWindow is nil");
}
}
_alertWindow = [[UIWindow alloc] initWithWindowScene:RCTKeyWindow().windowScene];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous check was also checking that the scene activation state was active.
can the windowScene retrieved by RCTKeyWindow be inactive (e.g.: there is already another alert presented)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've updated the function to take the activation state into account. As, we should always get the currently focused window. With the previous approach it was possible to retrieve some inactive keyWindow of additional scene (for example created by the OS). Can you check if the internal tests are passing now?


if (_alertWindow) {
_alertWindow.rootViewController = [UIViewController new];
Expand Down Expand Up @@ -65,16 +55,4 @@ - (void)hide
_alertWindow = nil;
}

- (UIWindow *)getUIWindowFromScene
{
for (UIScene *scene in RCTSharedApplication().connectedScenes) {
if (scene.activationState == UISceneActivationStateForegroundActive &&
[scene isKindOfClass:[UIWindowScene class]]) {
return [[UIWindow alloc] initWithWindowScene:(UIWindowScene *)scene];
}
}

return nil;
}

@end
9 changes: 4 additions & 5 deletions packages/react-native/React/CoreModules/RCTDevLoadingView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,13 @@ - (void)showMessage:(NSString *)message color:(UIColor *)color backgroundColor:(

dispatch_async(dispatch_get_main_queue(), ^{
self->_showDate = [NSDate date];

if (!self->_window && !RCTRunningInTestEnvironment()) {
UIWindow *window = RCTSharedApplication().keyWindow;
UIWindow *window = RCTKeyWindow();
CGFloat windowWidth = window.bounds.size.width;

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

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

self->_window.backgroundColor = backgroundColor;
self->_window.hidden = NO;

UIWindowScene *scene = (UIWindowScene *)RCTSharedApplication().connectedScenes.anyObject;
self->_window.windowScene = scene;
});

[self hideBannerAfter:15.0];
Expand Down