Skip to content

Commit 0c53420

Browse files
ouabingfacebook-github-bot
authored andcommitted
Fix RCTAlertController not showing when using SceneDelegate on iOS 13.0+ (#35716)
Summary: On iOS 13.0+, app may use SceneDelegate for multiple windows support or CarPlay support. RCTAlertController can't find the correct root vc in such scene based apps. ## Changelog <!-- Help reviewers and the release process by writing your own changelog entry. Pick one each for the category and type tags: [ANDROID|GENERAL|IOS|INTERNAL] [BREAKING|ADDED|CHANGED|DEPRECATED|REMOVED|FIXED|SECURITY] - Message For more details, see: https://reactnative.dev/contributing/changelogs-in-pull-requests --> [iOS] [Fixed] - Fix RCTAlertController not showing when using SceneDelegate on iOS 13.0+. Pull Request resolved: #35716 Reviewed By: cipolleschi Differential Revision: D42253653 Pulled By: makovkastar fbshipit-source-id: ae4e833abca2af7af8028f3af9bd8d3f60ebd392
1 parent 2217ea4 commit 0c53420

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

React/CoreModules/RCTAlertController.m

+26-6
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,21 @@ @implementation RCTAlertController
2020
- (UIWindow *)alertWindow
2121
{
2222
if (_alertWindow == nil) {
23-
UIWindow *keyWindow = RCTSharedApplication().keyWindow;
24-
if (keyWindow) {
25-
_alertWindow = [[UIWindow alloc] initWithFrame:keyWindow.bounds];
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+
}
34+
35+
if (_alertWindow) {
2636
_alertWindow.rootViewController = [UIViewController new];
2737
_alertWindow.windowLevel = UIWindowLevelAlert + 1;
28-
} else {
29-
// keyWindow is nil, so we cannot create and initialize _alertWindow
30-
NSLog(@"Unable to create alert window: keyWindow is nil");
3138
}
3239
}
3340

@@ -56,4 +63,17 @@ - (void)hide
5663
_alertWindow = nil;
5764
}
5865

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

0 commit comments

Comments
 (0)