diff --git a/React/CoreModules/RCTAlertController.h b/React/CoreModules/RCTAlertController.h new file mode 100644 index 0000000000..cd55e325a1 --- /dev/null +++ b/React/CoreModules/RCTAlertController.h @@ -0,0 +1,20 @@ +/* + * 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 // TODO(macOS ISS#2323203) + +#if TARGET_OS_OSX // [TODO(macOS ISS#2323203) +@interface RCTAlertController : NSViewController +#else +@interface RCTAlertController : UIAlertController +#endif // ]TODO(macOS ISS#2323203) + +#if !TARGET_OS_OSX // [TODO(macOS ISS#2323203) +- (void)show:(BOOL)animated completion:(void (^)(void))completion; +#endif // ]TODO(macOS ISS#2323203) + +@end diff --git a/React/CoreModules/RCTAlertController.m b/React/CoreModules/RCTAlertController.m new file mode 100644 index 0000000000..514c543edd --- /dev/null +++ b/React/CoreModules/RCTAlertController.m @@ -0,0 +1,40 @@ +/* + * 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 () + +#if !TARGET_OS_OSX // [TODO(macOS ISS#2323203) +@property (nonatomic, strong) UIWindow *alertWindow; +#endif // ]TODO(macOS ISS#2323203) + +@end + +@implementation RCTAlertController + +#if !TARGET_OS_OSX // [TODO(macOS ISS#2323203) +- (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]; +} +#endif // ]TODO(macOS ISS#2323203) + +@end diff --git a/React/CoreModules/RCTAlertManager.mm b/React/CoreModules/RCTAlertManager.mm index fafafc3e7c..b2cc12c7ff 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) @@ -115,29 +116,9 @@ RCT_EXPORT_METHOD(alertWithArgs : (JS::NativeAlertManager::Args &)args callback } } - 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) { @@ -186,7 +167,7 @@ RCT_EXPORT_METHOD(alertWithArgs : (JS::NativeAlertManager::Args &)args callback } else if ([buttonKey isEqualToString:destructiveButtonKey]) { buttonStyle = UIAlertActionStyleDestructive; } - __weak UIAlertController *weakAlertController = alertController; + __weak RCTAlertController *weakAlertController = alertController; [alertController addAction:[UIAlertAction actionWithTitle:buttonTitle @@ -218,7 +199,7 @@ RCT_EXPORT_METHOD(alertWithArgs : (JS::NativeAlertManager::Args &)args callback [_alertControllers addObject:alertController]; dispatch_async(dispatch_get_main_queue(), ^{ - [presentingController presentViewController:alertController animated:YES completion:nil]; + [alertController show:YES completion:nil]; }); #else // [TODO(macOS ISS#2323203)