[英]javascript alert and confirm popups not working with Webkit in IOS 9
WebKit無法使用Javascript彈出窗口。
在Safari(在OSX上)中,此文檔將創建預期的警報並確認彈出窗口。 但是,如果我的WebKit實例在IOS模擬器或設備上運行,則不會顯示彈出窗口,並且Confirm函數將返回false。
<!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>
我正在使用Xcode 7.3,針對IOS 9.3進行編譯。 info.plist
具有"AppTransportSecurity"
到"Allow arbitrary HTML"
。
這是我實例化Web視圖的代碼視圖控制器代碼:
import UIKit
import WebKit
class ViewController: UIViewController, WKNavigationDelegate {
@IBOutlet weak var wv: UIView!
var webView : WKWebView?
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(true)
let webViewCfg = WKWebViewConfiguration()
webViewCfg.preferences.javaScriptEnabled = true;
webViewCfg.preferences.javaScriptCanOpenWindowsAutomatically = true
webView = WKWebView.init(frame:wv.bounds, configuration: webViewCfg)
self.wv.addSubview(webView!)
webView!.navigationDelegate = self
let path = getBundlePath("js1.html")
let targetFileURL = NSURL(fileURLWithPath: path!, isDirectory: false)
webView!.loadFileURL(targetFileURL, allowingReadAccessToURL: targetFileURL)
}
}
從這個答案
您必須使用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)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.