项目中用到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();方法一定要实现 不然会崩溃。