【问题标题】:iOS : Connected to wifi but no internet accessiOS:已连接到 wifi 但无法访问互联网
【发布时间】:2018-03-19 18:55:31
【问题描述】:

我已启用移动数据 (3G/4G),但我的移动网络中没有数据。

是否可以快速知道上述情况。

我检查了下面的代码,但它给了我错误的信息。以下条件仅在关闭移动数据时才满足。我的要求是打开移动数据并且没有可用的互联网

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

    }

【问题讨论】:

  • 这是一个已知问题(?)。看herehere
  • 你说不可能吗
  • 是的,当前的可达性实现是不可能的。使用 hostName 初始化只是确认它是否是有效的主机名,但不会在运行时检查网络连接。
  • 有没有其他第三方可以解决这个问题
  • 为什么不点击网址,如果没有得到任何响应,可以说没有互联网连接。

标签: ios swift swift2 swift2.3


【解决方案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
}

【讨论】:

    猜你喜欢
    • 2015-12-14
    • 2017-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-29
    • 1970-01-01
    • 2019-11-23
    • 1970-01-01
    相关资源
    最近更新 更多