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..5f3c8cdc3ca48f --- /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:[[UIApplication sharedApplication] keyWindow].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