iOS Swift2.2可达主机IP

时间:2016-06-24 19:23:24

标签: ios iphone swift

我正在尝试检查localhost是否可以访问

 private static func checkHostaviable(){
    if ([[Reachability, reachabilityWithHostName,:"google.com"],currentReachabilityStatus] == false) {

    NSLog("not available");
    }
    else{
    NSLog("available");
    }
    }

但它并没有真正起作用..

任何想法?

1 个答案:

答案 0 :(得分:0)

这是常规方式和google方式(在Swift中):

    let reachability: Reachability = Reachability.reachabilityForInternetConnection()
    let networkStatus:NetworkStatus = reachability.currentReachabilityStatus()

    if  (networkStatus != NotReachable)
    {
        print("has access")
    }
    else
    {
        print("no access")
    }

    // Google explicit:
    let networkStatusGoogle = Reachability.init(hostName: "google.com").currentReachabilityStatus()
    if  (networkStatusGoogle != NotReachable)
    {
        print("has access to google")
    }
    else
    {
        print("no access to google")
    }

Objective-C方式是well documented by Apple,但此时你应该使用Swift。