[英]WKWebView - Javascript Confirm and Alert not working
我正在使用WKWebView打開example.com,在那兒我有一個測試鏈接,該鏈接應該打開JS警報,但是我無法在設備上顯示它,僅當我查看該網站時它才能工作從瀏覽器。
我正在使用WKUIDelegate,並將這段代碼添加到ViewController.swift文件中:
func webView(webView: WKWebView, runJavaScriptAlertPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: (() -> Void)) {
NSLog("Hello")
}
當我單擊生成JS警報的鏈接時,在XCode控制台中看不到任何內容。
我想念什么?
有點晚了,但我想補充一下我的經驗以供將來參考。 當我嘗試使Swift 3和IOS 10正常工作時,@ Bon Bon的答案將我帶到了解決方案的道路上,在這種情況下,代碼需要進行一些修改。 首先,您還需要實現WKUIDelegate
,因此將其添加到ViewController
聲明中:
class ViewController: UIViewController, WKUIDelegate {
然后,當您實例化WKWebView
對象時,例如這樣:
self.webView = WKWebView(frame: self.view.frame)
您還需要為實例的uiDelegate
屬性分配正確的值:
self.webView?.uiDelegate = self
然后,最后您可以使用@Bon Bon提供的代碼,但是請注意,Swift 3需要一些小的差異,例如, presentViewController
方法的名稱變為present
:
func webView(_ webView: WKWebView, runJavaScriptAlertPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping () -> Void) {
let alertController = UIAlertController(title: nil, message: message, preferredStyle: .actionSheet)
alertController.addAction(UIAlertAction(title: "Ok", style: .default, handler: { (action) in
completionHandler()
}))
self.present(alertController, animated: true, completion: nil)
}
func webView(_ webView: WKWebView, runJavaScriptConfirmPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping (Bool) -> Void) {
let alertController = UIAlertController(title: nil, message: message, preferredStyle: .actionSheet)
alertController.addAction(UIAlertAction(title: "Ok", style: .default, handler: { (action) in
completionHandler(true)
}))
alertController.addAction(UIAlertAction(title: "Cancel", style: .default, handler: { (action) in
completionHandler(false)
}))
self.present(alertController, animated: true, completion: nil)
}
func webView(_ webView: WKWebView, runJavaScriptTextInputPanelWithPrompt prompt: String, defaultText: String?, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping (String?) -> Void) {
let alertController = UIAlertController(title: nil, message: prompt, preferredStyle: .alert)
alertController.addTextField { (textField) in
textField.text = defaultText
}
alertController.addAction(UIAlertAction(title: "Ok", style: .default, handler: { (action) in
if let text = alertController.textFields?.first?.text {
completionHandler(text)
} else {
completionHandler(defaultText)
}
}))
alertController.addAction(UIAlertAction(title: "Cancel", style: .default, handler: { (action) in
completionHandler(nil)
}))
self.present(alertController, animated: true, completion: nil)
}
這使得alert
, confirmation
和text input
在WKWebView
可以正常工作,而Xcode 8中沒有任何編譯器警告。 我不是Swift專家,所以對代碼正確性的任何有用評論都將不勝感激。
您還需要在WKWebView上設置uiDelegate。
import UIKit
import WebKit
class ViewController: UIViewController, WKUIDelegate, WKNavigationDelegate {
var wkWebView: WKWebView!
public override func viewDidLoad() {
super.viewDidLoad()
wkWebView = WKWebView(frame: view.bounds, configuration: WKWebViewConfiguration())
wkWebView.uiDelegate = self
wkWebView.navigationDelegate = self
view.addSubview(wkWebView!)
let url = URL(string: "https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_alert")!
wkWebView.load(URLRequest(url: url))
}
func webView(_ webView: WKWebView,
runJavaScriptAlertPanelWithMessage message: String,
initiatedByFrame frame: WKFrameInfo,
completionHandler: @escaping () -> Void) {
let alert = UIAlertController(title: nil, message: message, preferredStyle: .alert)
let title = NSLocalizedString("OK", comment: "OK Button")
let ok = UIAlertAction(title: title, style: .default) { (action: UIAlertAction) -> Void in
alert.dismiss(animated: true, completion: nil)
}
alert.addAction(ok)
present(alert, animated: true)
completionHandler()
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
wkWebView.evaluateJavaScript("alert('Hello from evaluateJavascript()')", completionHandler: nil)
}
}
有關confirm()
和prompt()
請參見其他委托方法。
這是Swift中各種javascript警報實現的示例代碼:
這一切都是關於將javascript警報中的信息轉換為本機UI ,並調用completeHandler()將用戶操作發送回javascript引擎。
func webView(webView: WKWebView, runJavaScriptAlertPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: () -> Void) {
let alertController = UIAlertController(title: nil, message: message, preferredStyle: .ActionSheet)
alertController.addAction(UIAlertAction(title: "Ok", style: .Default, handler: { (action) in
completionHandler()
}))
self.presentViewController(alertController, animated: true, completion: nil)
}
func webView(webView: WKWebView, runJavaScriptConfirmPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: (Bool) -> Void) {
let alertController = UIAlertController(title: nil, message: message, preferredStyle: .ActionSheet)
alertController.addAction(UIAlertAction(title: "Ok", style: .Default, handler: { (action) in
completionHandler(true)
}))
alertController.addAction(UIAlertAction(title: "Cancel", style: .Default, handler: { (action) in
completionHandler(false)
}))
self.presentViewController(alertController, animated: true, completion: nil)
}
func webView(webView: WKWebView, runJavaScriptTextInputPanelWithPrompt prompt: String, defaultText: String?, initiatedByFrame frame: WKFrameInfo, completionHandler: (String?) -> Void) {
let alertController = UIAlertController(title: nil, message: prompt, preferredStyle: .ActionSheet)
alertController.addTextFieldWithConfigurationHandler { (textField) in
textField.text = defaultText
}
alertController.addAction(UIAlertAction(title: "Ok", style: .Default, handler: { (action) in
if let text = alertController.textFields?.first?.text {
completionHandler(text)
} else {
completionHandler(defaultText)
}
}))
alertController.addAction(UIAlertAction(title: "Cancel", style: .Default, handler: { (action) in
completionHandler(nil)
}))
self.presentViewController(alertController, animated: true, completion: nil)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.