From 41d9bdcce482d9143f08bee093e4ca901051dff2 Mon Sep 17 00:00:00 2001 From: npm-ued <31879256+npm-ued@users.noreply.github.com> Date: Fri, 14 Dec 2018 18:18:48 +0800 Subject: [PATCH] fix(WKWebview): Fixed non-working iOS alert. (#188) --- ios/RNCWKWebView.m | 83 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) diff --git a/ios/RNCWKWebView.m b/ios/RNCWKWebView.m index 375fe67..95ce13c 100644 --- a/ios/RNCWKWebView.m +++ b/ios/RNCWKWebView.m @@ -8,6 +8,7 @@ #import "RNCWKWebView.h" #import #import +#import #import "objc/runtime.h" @@ -339,6 +340,88 @@ static NSString *const MessageHanderName = @"ReactNative"; #pragma mark - WKNavigationDelegate methods +/** +* alert +*/ +- (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler +{ + UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:message preferredStyle:UIAlertControllerStyleAlert]; + [alert addAction:[UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { + completionHandler(); + }]]; + [[self topViewController] presentViewController:alert animated:YES completion:NULL]; + +} + +/** +* confirm +*/ +- (void)webView:(WKWebView *)webView runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(BOOL))completionHandler{ + UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:message preferredStyle:UIAlertControllerStyleAlert]; + [alert addAction:[UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { + completionHandler(YES); + }]]; + [alert addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { + completionHandler(NO); + }]]; + [[self topViewController] presentViewController:alert animated:YES completion:NULL]; +} + +/** +* prompt +*/ +- (void)webView:(WKWebView *)webView runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)defaultText initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(NSString *))completionHandler{ + UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:prompt preferredStyle:UIAlertControllerStyleAlert]; + [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) { + textField.textColor = [UIColor lightGrayColor]; + textField.placeholder = defaultText; + }]; + [alert addAction:[UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { + completionHandler([[alert.textFields lastObject] text]); + }]]; + [[self topViewController] presentViewController:alert animated:YES completion:NULL]; +} + +/** + * topViewController + */ +-(UIViewController *)topViewController{ +   UIViewController *controller = [self topViewControllerWithRootViewController:[self getCurrentWindow].rootViewController]; +   return controller; +} + +/** + * topViewControllerWithRootViewController + */ +-(UIViewController *)topViewControllerWithRootViewController:(UIViewController *)viewController{ + if (viewController==nil) return nil; + if (viewController.presentedViewController!=nil) { + return [self topViewControllerWithRootViewController:viewController.presentedViewController]; + } else if ([viewController isKindOfClass:[UITabBarController class]]){ + return [self topViewControllerWithRootViewController:[(UITabBarController *)viewController selectedViewController]]; + } else if ([viewController isKindOfClass:[UINavigationController class]]){ + return [self topViewControllerWithRootViewController:[(UINavigationController *)viewController visibleViewController]]; + } else { + return viewController; + } +} +/** + * getCurrentWindow + */ +-(UIWindow *)getCurrentWindow{ + UIWindow *window = [UIApplication sharedApplication].keyWindow; + if (window.windowLevel!=UIWindowLevelNormal) { + for (UIWindow *wid in [UIApplication sharedApplication].windows) { + if (window.windowLevel==UIWindowLevelNormal) { + window = wid; + break; + } + } + } + return window; +} + + /** * Decides whether to allow or cancel a navigation. * @see https://fburl.com/42r9fxob