diff --git a/RNTester/Podfile.lock b/RNTester/Podfile.lock index 0c0db2b311322f..1efd4f2087dde6 100644 --- a/RNTester/Podfile.lock +++ b/RNTester/Podfile.lock @@ -505,7 +505,7 @@ SPEC CHECKSUMS: RCTTypeSafety: 1ade47a69b092cddf1e4ea21e0c5bdc65cc950b4 React: cafb3c2321f7df55ce90dbf29d513799a79e4418 React-callinvoker: 0dada022d38b73e6e15b33e2a96476153f79bbf6 - React-Core: 22edbf19e3f430ce23111ead6741579f7a591ff2 + React-Core: 6c6f6c40bb1e031de8a0fafce08c010edfef09ab React-CoreModules: d13d148c851af5780f864be74bc2165140923dc7 React-cxxreact: 4661b3295e62c6eaada084e2f826c70c71ef11ea React-jsi: fe94132da767bfc4801968c2a12abae43e9a833e diff --git a/React/CoreModules/RCTAlertController.h b/React/CoreModules/RCTAlertController.h new file mode 100644 index 00000000000000..ada35e81fd846e --- /dev/null +++ b/React/CoreModules/RCTAlertController.h @@ -0,0 +1,14 @@ +/* + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#import + +@interface RCTAlertController : UIAlertController + +- (void)show:(BOOL)animated completion:(void (^)(void))completion; + +@end \ No newline at end of file diff --git a/React/CoreModules/RCTAlertController.m b/React/CoreModules/RCTAlertController.m new file mode 100644 index 00000000000000..e38c5005a8ff62 --- /dev/null +++ b/React/CoreModules/RCTAlertController.m @@ -0,0 +1,36 @@ +/* + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#import + +#import "RCTAlertController.h" + +@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 +{ + [self.alertWindow makeKeyAndVisible]; + [self.alertWindow.rootViewController presentViewController:self animated:animated completion:completion]; +} + +@end diff --git a/React/CoreModules/RCTAlertManager.mm b/React/CoreModules/RCTAlertManager.mm index 8a956fa71ab148..425951e60f8c3e 100644 --- a/React/CoreModules/RCTAlertManager.mm +++ b/React/CoreModules/RCTAlertManager.mm @@ -15,6 +15,7 @@ #import #import "CoreModulesPlugins.h" +#import "RCTAlertController.h" @implementation RCTConvert (UIAlertViewStyle) @@ -99,29 +100,9 @@ - (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 - message:nil - preferredStyle:UIAlertControllerStyleAlert]; + RCTAlertController *alertController = [RCTAlertController alertControllerWithTitle:title + message:nil + preferredStyle:UIAlertControllerStyleAlert]; switch (type) { case RCTAlertViewStylePlainTextInput: { [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) { @@ -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]; }); }