WKWebView---WKUIDelegate

weixin_34214500發表於2018-12-06
<!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];
}