17

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sam
  • 4,046
  • 8
  • 31
  • 47

4 Answers4

19

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)
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ramesh
  • 617
  • 6
  • 16
0

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()
        }
Yalamandarao
  • 3,852
  • 3
  • 20
  • 27
0

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;
}
Bartłomiej Semańczyk
  • 59,234
  • 49
  • 233
  • 358
Abhimanyu Daspan
  • 1,095
  • 16
  • 20
0

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)
}
Tunaki
  • 132,869
  • 46
  • 340
  • 423
Sunil Sharma
  • 2,653
  • 1
  • 25
  • 36