0

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.

Ashley Mills
  • 50,474
  • 16
  • 129
  • 160
TheFuquan
  • 1,737
  • 16
  • 18
  • check this url https://stackoverflow.com/questions/30743408/check-for-internet-connection-with-swift – Lalit kumar Aug 24 '17 at 09:11
  • Thanks @Lalitkumar, i have been all over stackoverflow before posting my question, all those questions don't tackle my own, i'm trying to identify internet acces and not wifi or 3/4G connections. thank you though :) – TheFuquan Aug 24 '17 at 09:19
  • Possible duplicate of [How to check for an active Internet connection on iOS or OSX?](https://stackoverflow.com/questions/1083701/how-to-check-for-an-active-internet-connection-on-ios-or-osx) – Devang Tandel Aug 24 '17 at 09:38

3 Answers3

1

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)
                }
            })
        }
    }
Anand Menon
  • 141
  • 5
1

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.

AlexK
  • 638
  • 6
  • 12
  • thank you for actually addressing the original question :) can you please reference the video of the talk, or even some official document ? – TheFuquan Aug 24 '17 at 15:35
0

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...
}
kelin
  • 11,323
  • 6
  • 67
  • 104
  • Thanks, but like i have been saying, i've done all that, my question was to confirm that the reachabiliy library only checks if there is a wifi connection and not actually checking if there is actual internet access coming through that wifi connection. – TheFuquan Aug 24 '17 at 09:29
  • If there is no internet access, you will get `NotReachable`, or I'm wrong? – kelin Aug 24 '17 at 11:51
  • That is exactly my issue, when i disconnect my router and connect my ios device to my router i keep getting ReachableViaWiFi as if the library is only checking if the device is connected via wifi to a router and not checking for internet connection. – TheFuquan Aug 24 '17 at 12:10