WKWebView - Javascript 确认和警报不起作用
WKWebView - Javascript Confirm and Alert not working
我正在使用 WKWebView 打开 example.com,在那里我有一个测试 link,它应该打开一个 JS 警报,但我无法让它显示在设备,只有当我从浏览器查看网站时它才有效。
我正在使用 WKUIDelegate,并将这段代码添加到 ViewController.swift 文件中:
func webView(webView: WKWebView, runJavaScriptAlertPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: (() -> Void)) {
NSLog("Hello")
}
当我单击生成 JS 警报的 link 时,我在 XCode 控制台中没有看到任何内容。
我错过了什么?
您还需要在 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,并调用 completionHandler() 将用户操作发送回 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)
}
有点晚了,但我想补充一下我的经验以供将来参考。当我尝试使用 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提供的代码了,但是需要注意的是Swift3有一些小的不同,比如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 打开 example.com,在那里我有一个测试 link,它应该打开一个 JS 警报,但我无法让它显示在设备,只有当我从浏览器查看网站时它才有效。
我正在使用 WKUIDelegate,并将这段代码添加到 ViewController.swift 文件中:
func webView(webView: WKWebView, runJavaScriptAlertPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: (() -> Void)) {
NSLog("Hello")
}
当我单击生成 JS 警报的 link 时,我在 XCode 控制台中没有看到任何内容。
我错过了什么?
您还需要在 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,并调用 completionHandler() 将用户操作发送回 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)
}
有点晚了,但我想补充一下我的经验以供将来参考。当我尝试使用 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提供的代码了,但是需要注意的是Swift3有一些小的不同,比如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 程序员,所以任何关于代码正确性的有用评论将不胜感激。