附上 demo 地址 https://github.com/Liuchunqi3240/demo/tree/master/WKWebView/HTMLStudy
自从学了HTML 之后 再用wkwebview 简直想说 真🐔2️⃣蔡
alert 不做处理的话 在wkwebview上竟然没有反应
是通过document.write()
这个函数起作用,而我写的一个alert()
没有反应发现的这个问题。
下面说一下解决方法
没事就问问百度
百度告诉我添加一下三个方法就好了
#pragma mark -- WKUIDelegate
// 显示一个按钮。点击后调用completionHandler回调
- (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:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}]];
[self presentViewController:alertController animated:YES completion:nil];
}
// 显示两个按钮,通过completionHandler回调判断用户点击的确定还是取消按钮
- (void)webView:(WKWebView *)webView runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(BOOL))completionHandler{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:message message:nil preferredStyle:UIAlertControllerStyleAlert];
[alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}]];
[alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}]];
[self presentViewController:alertController animated:YES completion:nil];
}
// 显示一个带有输入框和一个确定按钮的,通过completionHandler回调用户输入的内容
- (void)webView:(WKWebView *)webView runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)defaultText initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(NSString * _Nullable))completionHandler{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"" message:nil preferredStyle:UIAlertControllerStyleAlert];
[alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
}];
[alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
completionHandler(alertController.textFields.lastObject.text);
}]];
[self presentViewController:alertController animated:YES completion:nil];
}
然而事情并没有那么简单
直接给我扔出来一个错误,还一脸蒙蔽
webView:runJavaScriptAlertPanelWithMessage:initiatedByFrame:completionHandler:] was not called'、
然后就闪退了,你们抄袭能不能认真点,都不好用了 你们还放上去
正确的做法应该是这样子的
当然首先你要设置一下
wkwebview
的代理
self.webView.uiDelegate = self
// MARK: - WKUIDelegate
extension WKWebViewController: WKUIDelegate{
// show js alert
func webView(_ webView: WKWebView, runJavaScriptAlertPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping () -> Void) {
let alert = UIAlertController.init(title: message, message: nil, preferredStyle: .alert)
let action = UIAlertAction.init(title: "确定", style: .default) { (action) in
completionHandler()
}
alert.addAction(action)
self.present(alert, animated: true, completion: nil)
}
// show js commfirm
func webView(_ webView: WKWebView, runJavaScriptConfirmPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping (Bool) -> Void) {
let alert = UIAlertController.init(title: message, message: nil, preferredStyle: .alert)
let cancelAction = UIAlertAction.init(title: "取消", style: .cancel) { (action) in
completionHandler(false)
}
alert.addAction(cancelAction)
let sureAction = UIAlertAction.init(title: "确定", style: .default) { (action) in
completionHandler(true)
}
alert.addAction(sureAction)
self.present(alert, animated: true, completion: nil)
}
// show js Prompt
func webView(_ webView: WKWebView, runJavaScriptTextInputPanelWithPrompt prompt: String, defaultText: String?, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping (String?) -> Void) {
let alert = UIAlertController.init(title: "提示", message: prompt, preferredStyle: .alert)
alert.addTextField { (texfiled) in
texfiled.placeholder = defaultText
}
let sureAction = UIAlertAction.init(title: "确定", style: .default) { (action) in
completionHandler(alert.textFields?.last?.text ?? "")
}
alert.addAction(sureAction)
self.present(alert, animated: true, completion: nil)
}
}