iOS WKWebView关于js的alert方法不弹框的问题

项目中用到h5页面出现alert iOS端没有弹出信息的问题
解决办法如下

WKUIDelegate里面的方法

OC

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

swift

    //获取js alert事件
    func webView(_ webView: WKWebView, runJavaScriptAlertPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping () -> Void) {

        let alertController = UIAlertController.init(title: "提示", message: message, preferredStyle: .alert);
        alertController.addAction(UIAlertAction.init(title: "确定", style: .cancel, handler: { (action) in
            printLog("点击了。。。。");
            completionHandler();
        }))
        self.present(alertController, animated: true) {
            printLog("gone。。。。");
        }
//        printLog("message == \(message)");
//        completionHandler();
    }
    
    ```
    

> 注:completionHandler();方法一定要实现 不然会崩溃。

发布了121 篇原创文章 · 获赞 54 · 访问量 44万+

猜你喜欢

转载自blog.csdn.net/wm9028/article/details/90297554
今日推荐