From 153aedce413ef73f5e026abdfcf1346a37cec219 Mon Sep 17 00:00:00 2001 From: Tommy Nguyen <4123478+tido64@users.noreply.github.com> Date: Thu, 8 Sep 2022 12:54:08 -0700 Subject: [PATCH] Fix `Alert` not showing in an app using `UIScene` (#34562) Summary: In an app using `UIScene`, `Alert` no longer pops up because the window that gets created are not attached to a scene. ## Changelog [iOS] [Fixed] - Fix `Alert` not showing in an app using `UIScene` Pull Request resolved: https://github.com/facebook/react-native/pull/34562 Test Plan: Use the test plan in https://github.com/facebook/react-native/issues/29295. This should not regress that fix. Reviewed By: cipolleschi Differential Revision: D39276976 Pulled By: lunaleaps fbshipit-source-id: e48e985ed4abec77d6f01a6c17292d664ed88f13 --- React/CoreModules/RCTAlertController.h | 1 - React/CoreModules/RCTAlertController.m | 30 +------------------------- React/CoreModules/RCTAlertManager.mm | 3 --- 3 files changed, 1 insertion(+), 33 deletions(-) diff --git a/React/CoreModules/RCTAlertController.h b/React/CoreModules/RCTAlertController.h index 62adf2f3ac9..74e183da791 100644 --- a/React/CoreModules/RCTAlertController.h +++ b/React/CoreModules/RCTAlertController.h @@ -10,6 +10,5 @@ @interface RCTAlertController : UIAlertController - (void)show:(BOOL)animated completion:(void (^)(void))completion; -- (void)hide; @end diff --git a/React/CoreModules/RCTAlertController.m b/React/CoreModules/RCTAlertController.m index 02636c3f4a7..fc531b3a331 100644 --- a/React/CoreModules/RCTAlertController.m +++ b/React/CoreModules/RCTAlertController.m @@ -9,24 +9,8 @@ #import -@interface RCTAlertController () - -@property (nonatomic, strong) UIWindow *alertWindow; - -@end - @implementation RCTAlertController -- (UIWindow *)alertWindow -{ - if (_alertWindow == nil) { - _alertWindow = [[UIWindow alloc] initWithFrame:RCTSharedApplication().keyWindow.bounds]; - _alertWindow.rootViewController = [UIViewController new]; - _alertWindow.windowLevel = UIWindowLevelAlert + 1; - } - return _alertWindow; -} - - (void)show:(BOOL)animated completion:(void (^)(void))completion { if (@available(iOS 13.0, *)) { @@ -34,19 +18,7 @@ RCTSharedApplication().delegate.window.overrideUserInterfaceStyle ?: UIUserInterfaceStyleUnspecified; self.overrideUserInterfaceStyle = style; } - [self.alertWindow makeKeyAndVisible]; - [self.alertWindow.rootViewController presentViewController:self animated:animated completion:completion]; -} - -- (void)hide -{ - [_alertWindow setHidden:YES]; - - if (@available(iOS 13, *)) { - _alertWindow.windowScene = nil; - } - - _alertWindow = nil; + [[RCTKeyWindow() rootViewController] presentViewController:self animated:animated completion:completion]; } @end diff --git a/React/CoreModules/RCTAlertManager.mm b/React/CoreModules/RCTAlertManager.mm index 532e898faf0..25ab71f913b 100644 --- a/React/CoreModules/RCTAlertManager.mm +++ b/React/CoreModules/RCTAlertManager.mm @@ -185,7 +185,6 @@ RCT_EXPORT_METHOD(alertWithArgs : (JS::NativeAlertManager::Args &)args callback case RCTAlertViewStylePlainTextInput: case RCTAlertViewStyleSecureTextInput: callback(@[ buttonKey, [weakAlertController.textFields.firstObject text] ]); - [weakAlertController hide]; break; case RCTAlertViewStyleLoginAndPasswordInput: { NSDictionary *loginCredentials = @{ @@ -193,12 +192,10 @@ RCT_EXPORT_METHOD(alertWithArgs : (JS::NativeAlertManager::Args &)args callback @"password" : [weakAlertController.textFields.lastObject text] }; callback(@[ buttonKey, loginCredentials ]); - [weakAlertController hide]; break; } case RCTAlertViewStyleDefault: callback(@[ buttonKey ]); - [weakAlertController hide]; break; } }];