0

My app is based on a TableView that download some data from a server using Alamofire. So because it's necessary to have an internet connection to use my app I want to check it continuously. I find a solution creating this class:

class Connectivity {
class func isConnectedToInternet() -> Bool {
    return NetworkReachabilityManager()!.isReachable
}

}

And I add these lines of code in every method to check for Internet connection

if !Connectivity.isConnectedToInternet() {

        print("No internet connection")
        } else {
            print("connected")
        }

It works but I don't think that this is the right method to check continuously for the connection. I think that I've to implement some observer using notificationCenter from appDelegate but I don't know how to do it...

  • 2
    Actually you should _never_ check for connectivity / reachability. Just go ahead and network. If the Internet is not present / gone away, the connection will fail in good order and you can deal with it then. – matt Mar 15 '19 at 16:00
  • what do you mean? I need to display a label "no internet connection" like instagram does, so I need to check for connectivity... –  Mar 15 '19 at 16:02
  • And you will do that as soon as the failure error tells you there is no connection. No need to check specially. – matt Mar 15 '19 at 16:08
  • so are you saying that I have to check if the request with Alamofire fails or succeeds? and after that I have to show the label depending on the case? –  Mar 15 '19 at 16:12
  • Yes, but you don’t have to “check”: if you’ve set this up correctly, you are already set up to receive failure errors every time to try to talk to alamofire. – matt Mar 15 '19 at 16:14

2 Answers2

2

Do not do this! Apple has said for years that you should never use a reachability check as a prerequisite for making a request. Instead you should make the request and deal with the failure, using reachability to possibly retry the request when it detects connectivity has been reestablished. Reachability is not 100% reliable and is also now deprecated by the NWPathMonitor class.

Jon Shier
  • 12,200
  • 3
  • 35
  • 37
0

As Jon Shier and Matt say you shouldn't do this. In fact if you are using Alamofire to download an image I suggest you to using instead AlamofireImage and use this code:

let url = URL(string: yourUrl)!

        cell.yourImage.af_setImage(
            withURL: url,
            placeholderImage: placeholderImage,
            imageTransition: .crossDissolve(0.2),
            runImageTransitionIfCached: false,
            completion: { response in
                if response.result.isSuccess {
                    self.dismissLabel()
                } else if response.error?._code == NSURLErrorNotConnectedToInternet{
                    self.showLabel()
                }
        })

So basically you can show a label "No internet Connection" when AlamofireImage retrieves a connection error while downloading your image. Instead if it succeeded to download it you dismiss the label.

Andrea Culot
  • 114
  • 2
  • 9