UIWebview重新加载不起作用

时间:2017-07-24 09:56:03

标签: ios swift uiwebview reload

我使用UIWebView加载远程网址,由于网络不可用,网页为空。网络可用后,我重新加载UIWebView,但页面仍为空白。

我该如何解决?

2 个答案:

答案 0 :(得分:2)

试试这个

    func reloadRequest() {
        if let url = URL(string: "https://stackoverflow.com") {
            let request = URLRequest(url: url);
            self.loadRequest(request);
        }
    }

答案 1 :(得分:0)

再次加载带有请求的Web视图,例如:

if let url = URL(string: "https://www.google.com") {
            let request = URLRequest(url: url)
            wvWebView.loadRequest(request)
}

以下是示例代码。

@IBOutlet var wvWebView: UIWebView!

override func viewDidLoad() {
        super.viewDidLoad()
        wvWebView.delegate = self
        reloadWebview()
}


// call this function to reload web view.
func reloadWebview(){  
     if let url = URL(string: "https://www.google.com") {
            let request = URLRequest(url: url)
            wvWebView.loadRequest(request)
     }
}

// Webview Delegates
func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
        print("\n\n**** webView:shouldStartLoadWithRequest: \(request.url) \(navigationType)")
        return true
}


func webViewDidStartLoad(_ webView: UIWebView) {
        print("webViewDidStartLoad")
}


func webViewDidFinishLoad(_ webView: UIWebView) {
        print("webViewDidFinishLoad")
}

func webView(_ webView: UIWebView, didFailLoadWithError error: Error) {
    print("error description - \(error.localizedDescription)")
}