Browse Source

Fix alert not work in webview (#522)

* fix alert() not working in iOS webview

* inlcude alert() in example project
Jerry Zhou 5 years ago
parent
commit
75747251d4
2 changed files with 65 additions and 0 deletions
  1. 11 0
      example/lib/main.dart
  2. 54 0
      ios/Classes/FlutterWebviewPlugin.m

+ 11 - 0
example/lib/main.dart

@@ -273,6 +273,17 @@ class _MyHomePageState extends State<MyHomePage> {
               },
               child: const Text('Eval some javascript'),
             ),
+            RaisedButton(
+              onPressed: () {
+                final future = flutterWebViewPlugin.evalJavascript('alert("Hello World");');
+                future.then((String result) {
+                  setState(() {
+                    _history.add('eval: $result');
+                  });
+                });
+              },
+              child: const Text('Eval javascript alert()'),
+            ),
             RaisedButton(
               onPressed: () {
                 setState(() {

+ 54 - 0
ios/Classes/FlutterWebviewPlugin.m

@@ -477,4 +477,58 @@ static NSString *const CHANNEL_NAME = @"flutter_webview_plugin";
     }
 }
 
+#pragma mark -- WKUIDelegate
+- (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler
+{
+  UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil
+                                                                 message:message
+                                                          preferredStyle:UIAlertControllerStyleAlert];
+
+  [alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"OK", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
+    completionHandler();
+  }]];
+
+  [self.viewController presentViewController:alert animated:YES completion:nil];
+}
+
+- (void)webView:(WKWebView *)webView runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(BOOL result))completionHandler
+{
+  UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil
+                                                                 message:message
+                                                          preferredStyle:UIAlertControllerStyleAlert];
+
+  [alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
+    completionHandler(NO);
+  }]];
+
+  [alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"OK", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
+    completionHandler(YES);
+  }]];
+
+  [self.viewController presentViewController:alert animated:YES completion:nil];
+}
+
+- (void)webView:(WKWebView *)webView runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)defaultText initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(NSString *result))completionHandler
+{
+  UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil
+                                                                 message:prompt
+                                                          preferredStyle:UIAlertControllerStyleAlert];
+
+  [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
+    textField.placeholder = prompt;
+    textField.secureTextEntry = NO;
+    textField.text = defaultText;
+  }];
+
+  [alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
+    completionHandler(nil);
+  }]];
+
+  [alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"OK", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
+    completionHandler([alert.textFields.firstObject text]);
+  }]];
+
+  [self.viewController presentViewController:alert animated:YES completion:nil];
+}
+
 @end