Search code examples
iosswiftreachability

Using Reachability Class in Swift


I'm facing a problem with reachability class in Swift, I'm using the following code to get it to work, but something seems to be wrong..

import Foundation

class NetworkConnect {

    static let sharedInstance = NetworkConnect()

    private init() {
    }

    func connected() -> Bool {
        var reachability : Reachability = Reachability.reachabilityForInternetConnection()
        var networkStatus : NetworkStatus = reachability.currentReachabilityStatus
        if networkStatus.value == NotReachable.value  {
            return false
        } else {
            return true
        }
    }
    }

I am getting this error:

'Reachability.NetworkStatus' is not convertible to 'NetworkStatus'

How can I fix that?


Solution

  • Personally I would recommend you to use tonymillion's alternative to Reachability class, which can be found here:

    https://github.com/tonymillion/Reachability

    Then you can determine the connection in 1 line:

    let connected: Bool = Reachability.reachabilityForInternetConnection().isReachable()
    

    I would also recommend you to install this through CocoaPods if you have never tried that before. The reason being that Google will distribute all of their future iOS SDKs through CocoaPods, so you might start getting some practice with it already now :)