Search code examples
iosswiftreachability

How to use reachabilityWithHostName in Swift?


I'm trying to use the reachabilityWithHostName to test the server connection for the purpose of recognise whether "the server is down or not reachable".

However, there is an error:

'reachabilityWithHostName' is unavailable: use object construction 'Reachability(hostName:)'

Swift:

     let _HOST = "www.google.com" as NSString
     var netReach: Reachability = Reachability.reachabilityWithHostName(_HOST)
     var netStatus: NetworkStatus = netReach.currentReachabilityStatus()
        if (netStatus.value==ReachableViaWiFi.value) {

        } else if(netStatus.value==ReachableViaWWAN.value) {

        } else {
            println("notreachable")
        }

How can I use the function reachabilityWithHostName?

Thanks for any help.


Solution

  • Use

    let _HOST = "www.google.com" as NSString
    var netReach: Reachability = Reachability(hostName:_HOST)