From 4179beb830aacee4bbad74832e2ee8704d44a5f2 Mon Sep 17 00:00:00 2001 From: Devon Deonarine Date: Tue, 7 Jul 2020 23:43:09 -0400 Subject: [PATCH 1/2] iOS: Update RCTAlertManager to use new RCTAlertController This should fix these issues: https://github.com/facebook/react-native/issues/29082 https://github.com/facebook/react-native/issues/10471 --- React/CoreModules/RCTAlertManager.mm | 27 ++++----------------------- React/Views/RCTAlertController.h | 7 +++++++ React/Views/RCTAlertController.m | 25 +++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 23 deletions(-) create mode 100644 React/Views/RCTAlertController.h create mode 100644 React/Views/RCTAlertController.m diff --git a/React/CoreModules/RCTAlertManager.mm b/React/CoreModules/RCTAlertManager.mm index 8a956fa71ab148..b9c85f6b2679b6 100644 --- a/React/CoreModules/RCTAlertManager.mm +++ b/React/CoreModules/RCTAlertManager.mm @@ -13,6 +13,7 @@ #import #import #import +#import #import "CoreModulesPlugins.h" @@ -99,27 +100,7 @@ - (void)invalidate } } - UIViewController *presentingController = RCTPresentedViewController(); - if (presentingController == nil) { - RCTLogError(@"Tried to display alert view but there is no application window. args: %@", @{ - @"title" : args.title() ?: [NSNull null], - @"message" : args.message() ?: [NSNull null], - @"buttons" : RCTConvertOptionalVecToArray( - args.buttons(), - ^id(id element) { - return element; - }) - ?: [NSNull null], - @"type" : args.type() ?: [NSNull null], - @"defaultValue" : args.defaultValue() ?: [NSNull null], - @"cancelButtonKey" : args.cancelButtonKey() ?: [NSNull null], - @"destructiveButtonKey" : args.destructiveButtonKey() ?: [NSNull null], - @"keyboardType" : args.keyboardType() ?: [NSNull null], - }); - return; - } - - UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title + RCTAlertController *alertController = [RCTAlertController alertControllerWithTitle:title message:nil preferredStyle:UIAlertControllerStyleAlert]; switch (type) { @@ -170,7 +151,7 @@ - (void)invalidate } else if ([buttonKey isEqualToString:destructiveButtonKey]) { buttonStyle = UIAlertActionStyleDestructive; } - __weak UIAlertController *weakAlertController = alertController; + __weak RCTAlertController *weakAlertController = alertController; [alertController addAction:[UIAlertAction actionWithTitle:buttonTitle @@ -202,7 +183,7 @@ - (void)invalidate [_alertControllers addObject:alertController]; dispatch_async(dispatch_get_main_queue(), ^{ - [presentingController presentViewController:alertController animated:YES completion:nil]; + [alertController show:YES completion:nil]; }); } diff --git a/React/Views/RCTAlertController.h b/React/Views/RCTAlertController.h new file mode 100644 index 00000000000000..52dfd84694701f --- /dev/null +++ b/React/Views/RCTAlertController.h @@ -0,0 +1,7 @@ +#import + +@interface RCTAlertController : UIAlertController + +- (void)show:(BOOL)animated completion:(void (^)(void))completion; + +@end \ No newline at end of file diff --git a/React/Views/RCTAlertController.m b/React/Views/RCTAlertController.m new file mode 100644 index 00000000000000..9de9cf7d7ab8d1 --- /dev/null +++ b/React/Views/RCTAlertController.m @@ -0,0 +1,25 @@ +#import "RCTAlertController.h" + +@interface RCTAlertController () + +@property (nonatomic, strong) UIWindow *alertWindow; + +@end + +@implementation RCTAlertController + +- (UIWindow *)alertWindow { + if (_alertWindow == nil) { + _alertWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; + _alertWindow.rootViewController = [UIViewController new]; + _alertWindow.windowLevel = UIWindowLevelAlert + 1; + } + return _alertWindow; +} + +- (void)show:(BOOL)animated completion:(void (^)(void))completion { + [self.alertWindow makeKeyAndVisible]; + [self.alertWindow.rootViewController presentViewController:self animated:animated completion:completion]; +} + +@end \ No newline at end of file From 380b6217a7196f89029bc3330978967686dcc921 Mon Sep 17 00:00:00 2001 From: Devon Deonarine Date: Wed, 5 Aug 2020 23:08:38 -0400 Subject: [PATCH 2/2] use keyWindow bounds instead of mainScreen bounds --- React/Views/RCTAlertController.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/React/Views/RCTAlertController.m b/React/Views/RCTAlertController.m index 9de9cf7d7ab8d1..5f3c8cdc3ca48f 100644 --- a/React/Views/RCTAlertController.m +++ b/React/Views/RCTAlertController.m @@ -10,7 +10,7 @@ @implementation RCTAlertController - (UIWindow *)alertWindow { if (_alertWindow == nil) { - _alertWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; + _alertWindow = [[UIWindow alloc] initWithFrame:[[UIApplication sharedApplication] keyWindow].bounds]; _alertWindow.rootViewController = [UIViewController new]; _alertWindow.windowLevel = UIWindowLevelAlert + 1; }