简体   繁体   English

WKWebView中的主页按钮,Swift

[英]Home Button in WKWebView, Swift

I have a WkWebView that load an url (example.com) with 2 button: back and home. 我有一个WkWebView ,它使用2个按钮加载网址(example.com):返回和首页。

Code for back button is 后退按钮的代码是

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

and works fine. 并且工作正常。

Code for home button is 主页按钮的代码是

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

but this works also if I'm already in example.com. 但是如果我已经在example.com中,这也可以使用。

How can I make a check for reload example.com only if I'm not in example.com? 仅当我不在example.com中时,如何检查reload example.com?

You simply need to check against the web view's current URL. 您只需要检查Web视图的当前URL。 The following would suffice: 以下内容就足够了:

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

Though I would recommend adding an appropriate scheme and path to your URL else this will return false. 尽管我建议向您的URL添加适当的方案和路径,否则它将返回false。 eg URL(string: "https://example.com/") . 例如URL(string: "https://example.com/")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM