wcshinestar
wcshinestar

Reputation: 21

UIWebview reload doesn't work

I use UIWebView to load the remote url, the page is blank because the network is unavailable. After network is available ,i reload the UIWebView, but the page is still blank.

How can i solve it?

Upvotes: 2

Views: 1131

Answers (2)

Nikolay Khramchenko
Nikolay Khramchenko

Reputation: 421

Try this

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

Upvotes: 3

Krunal
Krunal

Reputation: 79636

Load web view with request again, like:

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

Here is sample code.

@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)")
}

Upvotes: 0

Related Questions