David David - 5 months ago 17
Swift Question

Call "didFailLoadWithError" when no internet connection?

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.

Answer

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.

EDIT: For Swift: TUTORIAL: CHECK FOR INTERNET CONNECTION IN SWIFT