【问题标题】:How to filter confirm dialogs from different URLs in iOS WKWebView如何在 iOS WKWebView 中过滤来自不同 URL 的确认对话框
【发布时间】:2020-04-01 05:19:21
【问题描述】:

我的网页有多个确认对话框。它们属于不同的网址

window.confirm("Hello") -> /hello
window.confirm("Exit") -> /confirm

我有我的视图控制器:

class View2Controller: UIViewController, WKUIDelegate {
var webView: WKWebView!
...
override func viewDidLoad() {
webView = WKWebView(frame: CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height), configuration: WKWebViewConfiguration())

        self.view.addSubview(webView)
        let myURL = URL(string:"https://www.myweb.com")
        let myRequest = URLRequest(url: myURL!)
        webView.uiDelegate = self
        webView.load(myRequest)

... ...

func webView(_ webView: WKWebView, runJavaScriptConfirmPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping (Bool) -> Void) {
            let ac = UIAlertController(title: "Title", message: message, preferredStyle: .alert)
            ac.addAction(UIAlertAction(title: "OK", style: .default, handler: { (UIAlertAction) in
                ...
            }))
            ac.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
            self.present(ac, animated: true)
        completionHandler(true)
        }

我是否可以仅在 /confirm URL 中使用 iOS 警报来确认退出,并为 Hello 使用网络确认对话框?

【问题讨论】:

    标签: ios swift xcode webview wkwebview


    【解决方案1】:

    当您使用WKWebView 时,没有“网络确认”对话框。所有对windowc.confirmwindow.alertwindow.prompt 的调用都会路由到您的WKUIDelegate,而这些调用会发生什么取决于您对各种委托方法的实现。

    也就是说,您没有在代码中正确使用 completionHandler - 它的参数应该反映用户所做的选择。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-10
      • 2021-12-12
      • 1970-01-01
      • 2010-09-16
      • 2010-10-27
      • 1970-01-01
      • 1970-01-01
      • 2023-04-01
      相关资源
      最近更新 更多