1

I already googled but didn't find a clear answer. When is "didFailLoadWithError" called? I want to use it to show an alert when there's no internet connection. Is that the right way?

It's for my iOS app made with Xcode and Swift.

rmaddy
  • 314,917
  • 42
  • 532
  • 579

1 Answers1

2

didFailLoadWithError method will get called in the following conditions:

  1. If the URL redirects to another URL, we will receive an NSURLErrorCancelled error.
  2. If the page contains links to the AppStore, tapping the link will return an error (but the AppStore link will still be handled by iOS).
  3. If the URL is a direct link to a Video/Audio, we will receive an error (“Plug-in handled load”) even if the video/audio will play.

So the best solution to check network is to use apple Reachability code.

Update:

The best solution to check for Internet connection (Wi-Fi and cellular) is: [Reachability Swift 2][2]

Satyam
  • 15,493
  • 31
  • 131
  • 244
  • Thanks for your answer. `didFailLoadWithError` also seems to be called when clicking a hyperlink while still loading another link. So, it's not what I'am locking for. The Reachability code seems to be the right. But it's only for Objective-C. Is there no Swift version? –  Jul 08 '16 at 11:41
  • For swift try this http://www.brianjcoleman.com/tutorial-check-for-internet-connection-in-swift/ – Palanichamy Jul 08 '16 at 12:18
  • Thank you. That is amazing. It's really working and sooo simple. I added the code in "webViewDidStartLoad". That means it'll always check for internet connection when tapping an hyperlink or so. Do you think it's reasonable? –  Jul 08 '16 at 13:18
  • This doesn't work for cellular/mobile data. Here's a working example for Wi-Fi/Cellular: http://www.filedropper.com/reachabilityswift2 –  Jul 10 '16 at 15:51