iOS:连接到wifi但没有互联网访问
问题描述:
我已启用我的移动数据(3G/4G),但我的移动网络中没有数据。iOS:连接到wifi但没有互联网访问
是否有可能迅速了解上述情况。
我检查了下面的代码,但它给了我错误的信息。以下条件仅在关闭移动数据时才满足。我的要求是移动数据已打开,但可用的编缉互联网
let reachability = Reachability(hostName: "https://www.google.com")
if reachability.currentReachabilityStatus() == NotReachable {
}
答
找到这工作对我来说
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
}
这是一个已知的问题的解决(?)。看看[这里](https://stackoverflow.com/questions/24260833/ios-reachabilitywithhostname-yes-although-it-should-be-no)和[这里](https://github.com/ashleymills/Reachability。 swift/issues/115) –
您是不是在告诉它不可能 – Sree
是的,目前的实现可达性是不可能的。使用hostName进行初始化只是确认它是否是有效的主机名,但不会检查运行时的网络连接。 –