swift - WKWebKit 不刷新网页

标签 swift macos cocoa wkwebview

我正在使用 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()
}

最佳答案

您可以使用导航委托(delegate)来确保在尝试加载另一个页面之前已完成对页面的导航。让您的类符合 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()
    }
}

关于swift - WKWebKit 不刷新网页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48108024/

相关文章:

ios - Firebase .childAdded 不起作用,因为观察到的 child 可能还不存在

ios - 不透明导航栏中的栏按钮未出现 ios

linux - 同时在同一操作系统上运行的两个终端窗口可以相互通信吗?

c - Mac OS X 中的 makefile 缺少分隔符

cocoa - 如何生成一组经过筛选的可用字体系列名称? (如在页面或文本编辑中)

ios - 在 tableView :editActionsForRowAt: 中处理图像

ios - 模拟器 iPhone X 系列 - 不断收到崩溃无法识别的选择器发送到所有按钮的实例

macos - 错误 : The 'brew link' step did not complete successfully

cocoa - Cocoa 中内置密码验证对话框?

cocoa 和OpenGL : fastest way to draw array of rectangles