【问题标题】:WKWebKit does not refresh webpageWKWebKit 不刷新网页
【发布时间】:2018-01-05 06:01:13
【问题描述】:

我正在使用 Xcode 8.3.3 和 Swift 3 为使用 Cocoa 的 iMac 开发应用程序。我的目标是使用VCgoToWebPage 并向用户显示网页。我的程序多次调用此函数,但我看到的唯一网页是最后一个调用的网页。如何在该函数内部实现窗口刷新并等待网页完全渲染?

func VCgoToWebPage(theWebPage : String) {
    let url = URL(string: theWebPage)!
    let request = URLRequest(url: url)
    webView.load(request)

    /*The modal box allows the web pages to be seen. Without it, after a series of calls to VCgoToWebPage only the last page called is displayed.  The modal box code is just for debugging and will be removed.  */

    let alert = NSAlert()
    alert.messageText="calling EDGAR page"
    alert.informativeText=theWebPage
    alert.addButton(withTitle: "OK")
    alert.runModal()
}

【问题讨论】:

    标签: swift macos cocoa wkwebview


    【解决方案1】:

    您可以使用导航委托来确保在尝试加载另一个页面之前完成导航到一个页面。让您的类符合 WKNavigationDelegate 并将 webView.navigationDelegate 设置为该类实例。

    var allRequests = [URLRequest]()
    func VCgoToWebPage(theWebPage : String) {
        guard let url = URL(string: theWebPage) else {
           return
        }
        let request = URLRequest(url: url)
        if webView.isLoading{
           allRequests.append(request)
        } else {
           webView.load(request)
        }
    }
    func webView(WKWebView, didFinish: WKNavigation!){
        if let nextRequest = allRequests.first{
           webView.load(nextRequest)
           allRequests.removeFirst()
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-30
      • 2014-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-01
      • 1970-01-01
      相关资源
      最近更新 更多