otter otter - 1 year ago 182
iOS Question

UIWebView offline error in Swift

I am loading a webpage into my app using UIWebView and following code:

let url = NSURL (string: "");
let requestObj = NSURLRequest(URL: url!);

I would like to hide webView if request failed for any reason, for example because of no access to internet. Is there a way to do it?

Answer Source

Use the provided delegate methods of UIWebView

func webViewDidFinishLoad(webView: UIWebView) {
    print("webview did finish load!")

func webView(webView: UIWebView, didFailLoadWithError error: NSError) {
    print("webview did fail load with error: \(error)")

Note: To use this method you need implement UIWebViewDelegate in your ViewController and set it with the webView outlet.

myWebView.delegate = self
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download