WKWebView 中的主页按钮,Swift

Home Button in WKWebView, Swift

我有一个 WkWebView 加载一个 url (example.com) 有 2 个按钮:返回和主页。

返回按钮的代码是

func goBack(_ sender: Any) {
    if webView.canGoBack{
        webView.goBack()
    }
}

并且工作正常。

主页按钮的代码是

func goHome(_ sender: Any) {
    let myURL = URL(string: "example.com")
    let myRequest = URLRequest(url: myURL!)
    webView.load(myRequest)
}

但如果我已经在 example.com.

,这也适用

只有当我不在 example.com 时,如何才能检查重新加载 example.com?

您只需检查网络视图的当前 URL。以下内容就足够了:

if webView.url != myURL {
    let myRequest = URLRequest(url: myURL!)
    webView.load(myRequest)
}

尽管我建议为您的 URL 添加适当的方案和路径,否则这将 return 错误。例如URL(string: "https://example.com/").