TheFuquan
TheFuquan

Reputation: 1797

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.

Upvotes: 0

Views: 742

Answers (3)

AlexK
AlexK

Reputation: 638

At WWDC, Apple has said many many times that if you need to simply see if WiFi or cell is up - use Reachable. If you need to see if you can connect to - then don’t use Reachable; instead simply connect and handle the error.

Reachable doesn’t actually check to see if it can connect to that IP based resource and if you are going to get a result back. The only way to do that is to actually try.

Look for networking sessions with Quinn on Apple’s WWDC site to see the same advice.

Upvotes: 1

Anand Menon
Anand Menon

Reputation: 141

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)
                }
            })
        }
    }

Upvotes: 1

kelin
kelin

Reputation: 12021

In the Reachablility.h you can find a declaration:

typedef enum : NSInteger {
    NotReachable = 0,
    ReachableViaWiFi,
    ReachableViaWWAN
} NetworkStatus;

Then, inside Reachability class you can find the method, which returns type:

- (NetworkStatus)currentReachabilityStatus;

So, use it. In your swift code you can do it like this:

if reachability.currentReachabilityStatus == .ReachableViaWiFi {
    // Do things...
}

Upvotes: 0

Related Questions