javascript警报和确认弹出窗口不适用于IOS 9中的Webkit
Javascript弹出窗口不适合我使用WebKit 在Safari(在OSX上)中,此文档将创建预期的警报和确认弹出窗口。但当我的WebKit实例在IOS模拟器或设备上运行时,不会显示弹出窗口,并且确认函数返回falsejavascript警报和确认弹出窗口不适用于IOS 9中的Webkit,javascript,ios,wkwebview,Javascript,Ios,Wkwebview,Javascript弹出窗口不适合我使用WebKit 在Safari(在OSX上)中,此文档将创建预期的警报和确认弹出窗口。但当我的WebKit实例在IOS模拟器或设备上运行时,不会显示弹出窗口,并且确认函数返回false <!DOCTYPE html> <html> <body> <p id="check3"></p> <script> alert("alert 1");
<!DOCTYPE html>
<html>
<body>
<p id="check3"></p>
<script>
alert("alert 1");
function check3() {
return(confirm("Confirm?"));
}
</script>
<button type="button"
onclick="document.getElementById('check3').innerHTML = check3()">
Check 3</button>
</body>
</html>
由此
您必须为webview委托执行以下操作:
// MARK: WKUIDelegate methods
func webView(webView: WKWebView, runJavaScriptAlertPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: (() -> Void)) {
print("webView:\(webView) runJavaScriptAlertPanelWithMessage:\(message) initiatedByFrame:\(frame) completionHandler:\(completionHandler)")
let alertController = UIAlertController(title: frame.request.URL?.host, message: message, preferredStyle: .Alert)
alertController.addAction(UIAlertAction(title: "OK", style: .Default, handler: { action in
completionHandler()
}))
self.presentViewController(alertController, animated: true, completion: nil)
}
请这样说:我为你写了swift版本,非常感谢你。我花了很长时间寻找这个问题的答案,但由于某种原因,我从未找到另一个答案。非常感谢swift版本。请将我的答案标记为正确,如果它对您有效,我将不胜感激,谢谢:)完成。再次感谢你的帮助。
// MARK: WKUIDelegate methods
func webView(webView: WKWebView, runJavaScriptAlertPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: (() -> Void)) {
print("webView:\(webView) runJavaScriptAlertPanelWithMessage:\(message) initiatedByFrame:\(frame) completionHandler:\(completionHandler)")
let alertController = UIAlertController(title: frame.request.URL?.host, message: message, preferredStyle: .Alert)
alertController.addAction(UIAlertAction(title: "OK", style: .Default, handler: { action in
completionHandler()
}))
self.presentViewController(alertController, animated: true, completion: nil)
}