如果 Webview 无法加载 URL,请切换视图?
switch view if the Webview can't loading the URL?
我的屏幕上有两个视图,第一个是 Webview,第二个是静态视图“errorView”,错误消息“设置为隐藏”。如果我们在将网站加载到 Webview 时出错,我正在尝试加载 errorView。
@objc private func loadURL() {
guard let url = URL(string: "https://www.hackingwi") else {
self.errroLoadingWebView.isHidden = false
return
}
webView?.load(URLRequest(url: url))
webView?.allowsBackForwardNavigationGestures = true
}
您可以使用 WKNavigationDelegate
.
设置委托
webView.uiDelegate = self
webView.navigationDelegate = self
class StaticWebPageVC: WKNavigationDelegate, WKUIDelegate {
func webView(_: WKWebView, didFinish _: WKNavigation!) {
// Web view loaded
}
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
// Show error view
}
func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
// Show error view
}
}
我的屏幕上有两个视图,第一个是 Webview,第二个是静态视图“errorView”,错误消息“设置为隐藏”。如果我们在将网站加载到 Webview 时出错,我正在尝试加载 errorView。
@objc private func loadURL() {
guard let url = URL(string: "https://www.hackingwi") else {
self.errroLoadingWebView.isHidden = false
return
}
webView?.load(URLRequest(url: url))
webView?.allowsBackForwardNavigationGestures = true
}
您可以使用 WKNavigationDelegate
.
设置委托
webView.uiDelegate = self
webView.navigationDelegate = self
class StaticWebPageVC: WKNavigationDelegate, WKUIDelegate {
func webView(_: WKWebView, didFinish _: WKNavigation!) {
// Web view loaded
}
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
// Show error view
}
func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
// Show error view
}
}