Skip to content

Commit 7574725

Browse files
authored
Fix alert not work in webview (#522)
* fix alert() not working in iOS webview * inlcude alert() in example project
1 parent 2e909be commit 7574725

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

example/lib/main.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,17 @@ class _MyHomePageState extends State<MyHomePage> {
273273
},
274274
child: const Text('Eval some javascript'),
275275
),
276+
RaisedButton(
277+
onPressed: () {
278+
final future = flutterWebViewPlugin.evalJavascript('alert("Hello World");');
279+
future.then((String result) {
280+
setState(() {
281+
_history.add('eval: $result');
282+
});
283+
});
284+
},
285+
child: const Text('Eval javascript alert()'),
286+
),
276287
RaisedButton(
277288
onPressed: () {
278289
setState(() {

ios/Classes/FlutterWebviewPlugin.m

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,4 +477,58 @@ - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
477477
}
478478
}
479479

480+
#pragma mark -- WKUIDelegate
481+
- (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler
482+
{
483+
UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil
484+
message:message
485+
preferredStyle:UIAlertControllerStyleAlert];
486+
487+
[alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"OK", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
488+
completionHandler();
489+
}]];
490+
491+
[self.viewController presentViewController:alert animated:YES completion:nil];
492+
}
493+
494+
- (void)webView:(WKWebView *)webView runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(BOOL result))completionHandler
495+
{
496+
UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil
497+
message:message
498+
preferredStyle:UIAlertControllerStyleAlert];
499+
500+
[alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
501+
completionHandler(NO);
502+
}]];
503+
504+
[alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"OK", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
505+
completionHandler(YES);
506+
}]];
507+
508+
[self.viewController presentViewController:alert animated:YES completion:nil];
509+
}
510+
511+
- (void)webView:(WKWebView *)webView runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)defaultText initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(NSString *result))completionHandler
512+
{
513+
UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil
514+
message:prompt
515+
preferredStyle:UIAlertControllerStyleAlert];
516+
517+
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
518+
textField.placeholder = prompt;
519+
textField.secureTextEntry = NO;
520+
textField.text = defaultText;
521+
}];
522+
523+
[alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
524+
completionHandler(nil);
525+
}]];
526+
527+
[alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"OK", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
528+
completionHandler([alert.textFields.firstObject text]);
529+
}]];
530+
531+
[self.viewController presentViewController:alert animated:YES completion:nil];
532+
}
533+
480534
@end

0 commit comments

Comments
 (0)