From a2288e0e851d4cd589e3f8d603ea438ae0b7fc9e Mon Sep 17 00:00:00 2001 From: jsharp83 Date: Fri, 21 Jul 2023 10:55:31 +0900 Subject: [PATCH] [webview_flutter] add functions to display a system popup using Javascript in iOS --- .../ios/Classes/FWFUIDelegateHostApi.m | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/packages/webview_flutter/webview_flutter_wkwebview/ios/Classes/FWFUIDelegateHostApi.m b/packages/webview_flutter/webview_flutter_wkwebview/ios/Classes/FWFUIDelegateHostApi.m index a7010509ef2..3b02b42c5ab 100644 --- a/packages/webview_flutter/webview_flutter_wkwebview/ios/Classes/FWFUIDelegateHostApi.m +++ b/packages/webview_flutter/webview_flutter_wkwebview/ios/Classes/FWFUIDelegateHostApi.m @@ -133,6 +133,62 @@ - (void)webView:(WKWebView *)webView decisionHandler(decision); }]; } + +- (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]; +} + +- (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]; + completionHandler(true); +} + +-(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; +} @end @interface FWFUIDelegateHostApiImpl () @@ -164,4 +220,6 @@ - (void)createWithIdentifier:(nonnull NSNumber *)identifier instanceManager:self.instanceManager]; [self.instanceManager addDartCreatedInstance:uIDelegate withIdentifier:identifier.longValue]; } + + @end