Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

iOS: Update RCTAlertManager to use new RCTAlertController #29295

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 4 additions & 23 deletions React/CoreModules/RCTAlertManager.mm
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#import <React/RCTConvert.h>
#import <React/RCTLog.h>
#import <React/RCTUtils.h>
#import <React/RCTAlertController.h>

#import "CoreModulesPlugins.h"

Expand Down Expand Up @@ -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<NSObject> 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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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];
});
}

Expand Down
7 changes: 7 additions & 0 deletions React/Views/RCTAlertController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#import <UIKit/UIKit.h>

@interface RCTAlertController : UIAlertController

- (void)show:(BOOL)animated completion:(void (^)(void))completion;

@end
25 changes: 25 additions & 0 deletions React/Views/RCTAlertController.m
Original file line number Diff line number Diff line change
@@ -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