2017-10-08 137 views
1

我已啓用我的移動數據(3G/4G),但我的移動網絡中沒有數據。iOS:連接到wifi但沒有互聯網訪問

是否有可能迅速瞭解上述情況。

我檢查了下面的代碼,但它給了我錯誤的信息。以下條件僅在關閉移動數據時才滿足。我的要求是移動數據已打開,但可用的編緝互聯網

let reachability = Reachability(hostName: "https://www.google.com") 
    if reachability.currentReachabilityStatus() == NotReachable { 

    } 
+0

這是一個已知的問題的解決(?)。看看[這裏](https://stackoverflow.com/questions/24260833/ios-reachabilitywithhostname-yes-although-it-should-be-no)和[這裏](https://github.com/ashleymills/Reachability。 swift/issues/115) –

+0

您是不是在告訴它不可能 – Sree

+0

是的,目前的實現可達性是不可能的。使用hostName進行初始化只是確認它是否是有效的主機名,但不會檢查運行時的網絡連接。 –

回答

1

找到這工作對我來說

func isConnectedToNetwork()->Bool { 
    var Status:Bool = false 
    let url = NSURL(string: "http://google.com/") 
    let request = NSMutableURLRequest(URL: url!) 
    request.HTTPMethod = "HEAD" 
    request.cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringLocalAndRemoteCacheData 
    request.timeoutInterval = 10.0 
    var response: NSURLResponse? 
    _ = try? NSURLConnection.sendSynchronousRequest(request, returningResponse: &response) as NSData? 
    if let httpResponse = response as? NSHTTPURLResponse { 
     if httpResponse.statusCode == 200 { 
      Status = true 
     } 
    } 
    return Status 
} 
相關問題