Sam
Sam

Reputation: 4284

Using Apple's reachability class in Swift

I am rewriting my existing Objective-C code (iOS) to Swift and now am facing some issues with the Reachability class of Apple for checking network availability... In my existing code I am using the following for achieving that.

var reachability: Reachability = Reachability.reachabilityForInternetConnection()
var internetStatus:NetworkStatus = reachability.currentReachabilityStatus()
if (internetStatus != NotReachable) {
    //my web-dependent code
}
else {
    //There-is-no-connection warning
}

And I am getting this error: network status is not convertible to string at this line:

if (internetStatus != NotReachable)

Is there a method or class for getting network status?

I need these three conditions:

NotReachable: Obviously, there’s no Internet connection
ReachableViaWiFi: Wi-Fi connection
ReachableViaWWAN: 3G or 4G connection

Upvotes: 17

Views: 29286

Answers (4)

Sunil Sharma
Sunil Sharma

Reputation: 2689

Simply use like this

do {
    let reachability: Reachability = try Reachability.reachabilityForInternetConnection()

     switch reachability.currentReachabilityStatus{
     case .ReachableViaWiFi:
         print("Connected With wifi")
     case .ReachableViaWWAN:
         print("Connected With Cellular network(3G/4G)")
     case .NotReachable:
         print("Not Connected")
     }
}
catch let error as NSError{
    print(error.debugDescription)
}

Upvotes: 0

Abhimanyu Daspan
Abhimanyu Daspan

Reputation: 1115

put this code in to your appDelegate for checking reachability .

//MARK: reachability class
func checkNetworkStatus() -> Bool {
    let reachability: Reachability = Reachability.reachabilityForInternetConnection()
    let networkStatus = reachability.currentReachabilityStatus().rawValue;
    var isAvailable  = false;

    switch networkStatus {
    case (NotReachable.rawValue):
        isAvailable = false;
        break;
    case (ReachableViaWiFi.rawValue):
        isAvailable = true;
        break;
    case (ReachableViaWWAN.rawValue):
        isAvailable = true;
        break;
    default:
        isAvailable = false;
        break;
    }
    return isAvailable;
}

Upvotes: 0

Ramesh
Ramesh

Reputation: 617

For network availability (works in Swift 2):

class func hasConnectivity() -> Bool {
    let reachability: Reachability = Reachability.reachabilityForInternetConnection()
    let networkStatus: Int = reachability.currentReachabilityStatus().rawValue
    return networkStatus != 0
}

For a Wi-Fi connection:

(reachability.currentReachabilityStatus().value == ReachableViaWiFi.value)

Upvotes: 19

Yalamandarao
Yalamandarao

Reputation: 3862

Try below code

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

        if connected == true {
             println("Internet connection OK")
        }
        else
        {
            println("Internet connection FAILED")
            var alert = UIAlertView(title: "No Internet Connection", message: "Make sure your device is connected to the internet.", delegate: nil, cancelButtonTitle: "OK")
            alert.show()
        }

Upvotes: 0

Related Questions