Ad

Check Internet Connection When User Interacts

- 1 answer

Where I should implement a function to check internet connection on user's iPhone every time? Exactly:

  • when some ViewController appears
  • when user goes to home screen and returns to app via multitasking

I have a lot of .swift files and I don't want to implement code in every viewWillApear function of every .swift file. Maybe there is some native place somewhere in AppDelegate or else?

Ad

Answer

Where I should implement a function to check internet connection on user's iPhone every time?

You shouldn't. The way to ask if the network is working is simply to try to use the network. You just attempt to do your networking and either you succeed or you fail in good order, and you take it from there. Most networking is done with URLSession, which will handle this gracefully for you, and in iOS 11 can even be configured to try again later automatically (waitsForConnectivity). Please watch the relevant WWDC 2017 video for further discussion.

Ad
source: stackoverflow.com
Ad