iosswiftreachabilityreachability-swift

Checking for internet connection or wifi connection with reachabilitySwift


I'm building an iOS app, at some point i needed to check the app's acces to internet, so i used the ReachabilitySwift library.

After some tests, it seems to me that the library only checks if the device has the wifi connected and not having an actual internet connection provided from my router.

lets say i disconnected my router from internet, if my iOS device is connected via wifi to the router, the reachability tells me that we have internet connection where in actuallity my router has no internet.

I tried the reachability with a particular host but still having the same result

var reachability = Reachability.init(hostname: "google.com");

Is the reachability library supposed to give feedback when the wifi connection is lost or the actual internet connection is lost ?

Thank you in advance.


Solution

  • I have had similar issues with Reachability where I was making web service calls to VPN protected network.

    var reachability = Reachability.init(hostname: "google.com"); didnt work for me as it returned true when there is Wifi connection.

    I used Alamofire response when a dummy call is made to the server

    func networkIsAvailable(available: (Bool, String?) -> Void) {
    
            var message : String = ""
            DispatchQueue.main.async {
                HUD.show(.progress)
                Alamofire.request(Constants.baseUrl, method: .get, parameters: ["foo": "bar"])
                .responseJSON(completionHandler: { (response) in
                    let error = response.result.error as? NSError
                    if error?.localizedDescription == networkAlert {
                        message = networkAlert
                        available(false, message)
    
                    } else if error?.code == cannotConnectServerCode || error?.localizedDescription == cannotConnectServerMessage {
                        message = cannotConnectServerMessage
                        available(false, anotherNetworkAlert)
    
                    } else {
                        available(true, nil)
                    }
                })
            }
        }