From 23d0ade5fa8183673dfec1894e508b50a96e69a1 Mon Sep 17 00:00:00 2001 From: abing Date: Sun, 25 Dec 2022 12:51:52 +0800 Subject: [PATCH 1/2] Fix RCTAlertController not showing when using SceneDelegate on iOS 13.0+ --- React/CoreModules/RCTAlertController.m | 29 ++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/React/CoreModules/RCTAlertController.m b/React/CoreModules/RCTAlertController.m index 36730b4c05491d..dcb0cc51508a1e 100644 --- a/React/CoreModules/RCTAlertController.m +++ b/React/CoreModules/RCTAlertController.m @@ -20,14 +20,31 @@ @implementation RCTAlertController - (UIWindow *)alertWindow { if (_alertWindow == nil) { - UIWindow *keyWindow = RCTSharedApplication().keyWindow; - if (keyWindow) { - _alertWindow = [[UIWindow alloc] initWithFrame:keyWindow.bounds]; +#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && defined(__IPHONE_13_0) && \ + __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_13_0 + if (@available(iOS 13.0, *)) { + for (UIScene *scene in RCTSharedApplication().connectedScenes) { + if (scene.activationState == UISceneActivationStateForegroundActive && [scene isKindOfClass:[UIWindowScene class]]) { + _alertWindow = [[UIWindow alloc] initWithWindowScene:(UIWindowScene *)scene]; + break; + } + } + } +#endif + + 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"); + } + } + + if (_alertWindow) { _alertWindow.rootViewController = [UIViewController new]; _alertWindow.windowLevel = UIWindowLevelAlert + 1; - } else { - // keyWindow is nil, so we cannot create and initialize _alertWindow - NSLog(@"Unable to create alert window: keyWindow is nil"); } } From 03de6ebafdf31a3dab112273a130b89770591c1c Mon Sep 17 00:00:00 2001 From: abing Date: Sat, 31 Dec 2022 13:32:16 +0800 Subject: [PATCH 2/2] Extract scene related logic into function in RCTAlertController --- React/CoreModules/RCTAlertController.m | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/React/CoreModules/RCTAlertController.m b/React/CoreModules/RCTAlertController.m index dcb0cc51508a1e..f66add9624e97c 100644 --- a/React/CoreModules/RCTAlertController.m +++ b/React/CoreModules/RCTAlertController.m @@ -20,17 +20,7 @@ @implementation RCTAlertController - (UIWindow *)alertWindow { if (_alertWindow == nil) { -#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && defined(__IPHONE_13_0) && \ - __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_13_0 - if (@available(iOS 13.0, *)) { - for (UIScene *scene in RCTSharedApplication().connectedScenes) { - if (scene.activationState == UISceneActivationStateForegroundActive && [scene isKindOfClass:[UIWindowScene class]]) { - _alertWindow = [[UIWindow alloc] initWithWindowScene:(UIWindowScene *)scene]; - break; - } - } - } -#endif + _alertWindow = [self getUIWindowFromScene]; if (_alertWindow == nil) { UIWindow *keyWindow = RCTSharedApplication().keyWindow; @@ -73,4 +63,16 @@ - (void)hide _alertWindow = nil; } +- (UIWindow *)getUIWindowFromScene +{ + if (@available(iOS 13.0, *)) { + for (UIScene *scene in RCTSharedApplication().connectedScenes) { + if (scene.activationState == UISceneActivationStateForegroundActive && [scene isKindOfClass:[UIWindowScene class]]) { + return [[UIWindow alloc] initWithWindowScene:(UIWindowScene *)scene]; + } + } + } + return nil; +} + @end