Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIWebView offline error in Swift

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

let url = NSURL (string: "http://www.myurl.com");
let requestObj = NSURLRequest(URL: url!);
myWebView.loadRequest(requestObj);

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?

like image 593
otter Avatar asked Feb 14 '26 07:02

otter


1 Answers

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
like image 103
Nirav D Avatar answered Feb 16 '26 21:02

Nirav D



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!