美文网首页
WKWebView---WKUIDelegate

WKWebView---WKUIDelegate

作者: 初灬终 | 来源:发表于2018-12-06 21:04 被阅读5次
<!DOCTYPE html>
<html>
<head>
    <title>webView</title>
    <style>
        button {
            font-size: 30px;
            width: 250px;
            height: 50px;
            display: block;
            margin-top: 5px;
        }
    </style>
    <script type="text/javascript">
        function showAlert() {
            alert("helloworld");
        }
        function showConfirm() {
            var confirmButton = document.getElementById("confirmButton");
            var bool = window.confirm("Are you OK?");
            if (bool) {
                confirmButton.style.background = "green";
            } else{
                confirmButton.style.background = "red";
            }
        }
        function showPrompt() {
            var name = prompt("hello, boy!", "your name--");
        }
    </script>
</head>
<body>
    <button onclick="showAlert()" id="alertButton">showAlert</button>
    <button onclick="showConfirm()" id="confirmButton">showConfirm</button>
    <button onclick="showPrompt()" id="promptButton">showPrompt</button>
</body>
</html>

自定义WKWebView的alert,每次在html点击showAlert的button时,都会调用此代理。在代理方法里,可以定义弹出的alertController

- (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler {
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"Pay Attension" message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"OK");
        completionHandler();
    }];
    [alertVC addAction:okAction];
    [self presentViewController:alertVC animated:YES completion:nil];
}

WKWebView的Confirm代理,每次在html展示Confirm弹窗时,都会被调用。
js代码里confirm(question: String): Boolean,Boolean便是OC的completionHandler(bool)回调时传入的参数。

- (void)webView:(WKWebView *)webView runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(BOOL))completionHandler {
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"Pay Attension" message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"OK");
        completionHandler(YES);
    }];
    UIAlertAction *cancalAction = [UIAlertAction actionWithTitle:@"cacel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"cancel");
        completionHandler(NO);
    }];
    [alertVC addAction:okAction];
    [alertVC addAction:cancalAction];
    [self presentViewController:alertVC animated:YES completion:nil];
}

WKWebView的Prompt代理,每次在html展示Prompt弹窗时,都会被调用。
js里prompt(message: String, default: String): String, Number,message, default便是OC的completionHandler(txt)回调时传入的参数。

- (void)webView:(WKWebView *)webView runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)defaultText initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(NSString * _Nullable))completionHandler {
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"Please Input!" message:prompt preferredStyle:(UIAlertControllerStyleAlert)];
    [alertVC addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        textField.placeholder = @"input";
    }];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        UITextField *tf = [alertVC.textFields firstObject];
        completionHandler(tf.text);
    }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"cacel" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
        completionHandler(defaultText);
    }];
    [alertVC addAction:okAction];
    [alertVC addAction:cancelAction];
    [self presentViewController:alertVC animated:YES completion:nil];
}

相关文章

  • WKWebView---WKUIDelegate

    自定义WKWebView的alert,每次在html点击showAlert的button时,都会调用此代理。在代理...

网友评论

      本文标题:WKWebView---WKUIDelegate

      本文链接:https://www.haomeiwen.com/subject/losacqtx.html