Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/119.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
当我连接到移动热点iOS Swift时如何检查网络连接_Ios_Swift - Fatal编程技术网

当我连接到移动热点iOS Swift时如何检查网络连接

当我连接到移动热点iOS Swift时如何检查网络连接,ios,swift,Ios,Swift,在我的应用程序中,我集成了rechability.swift。连接wifi时工作正常,但我想测试一个场景,比如连接到手机(没有互联网连接的热点)。当这个代码总是像connected一样执行时,因为状态是connected with wifi,但是我需要检查网络是否可用。还有其他的标准方式吗?请帮忙 using rechability same result let reachability: Reachability do { // reachability =

在我的应用程序中,我集成了
rechability.swift
。连接wifi时工作正常,但我想测试一个场景,比如连接到手机(没有互联网连接的热点)。当这个代码总是像connected一样执行时,因为状态是connected with wifi,但是我需要检查网络是否可用。还有其他的标准方式吗?请帮忙

using rechability same result

    let reachability: Reachability
    do {
        // reachability = try Reachability.NetworkReachable()

        reachability = try Reachability.reachabilityForInternetConnection()

        if (reachability.isReachableViaWiFi()) {
            _label.text = "wifi connected"
        }
        else if (reachability.isReachable()) {
            _label.text = "reachable"
        }

        else if (reachability.isReachableViaWWAN()) {
            _label.text = "connected via wan"

        }
        else {
            _label.text = "not connected "
        }
    } catch {
        print("Unable to create Reachability")
        return
    }
我使用的其他代码给出了相同的结果:

  func isConnectedToNetwork() -> Bool {

  var zeroAddress = sockaddr_in()
  zeroAddress.sin_len = UInt8(sizeofValue(zeroAddress))
  zeroAddress.sin_family = sa_family_t(AF_INET)
  let defaultRouteReachability = withUnsafePointer(&zeroAddress) {
    SCNetworkReachabilityCreateWithAddress(nil, UnsafePointer($0))
  }
  var flags = SCNetworkReachabilityFlags()
  if !SCNetworkReachabilityGetFlags(defaultRouteReachability!, &flags)    {
    return false
  }
  let isReachable = flags.contains(.Reachable)
  let needsConnection = flags.contains(.ConnectionRequired)
  //    let isReachable = (flags.rawValue & UInt32(kSCNetworkFlagsReachable)) != 0
  //    let needsConnection = (flags.rawValue &   UInt32(kSCNetworkFlagsConnectionRequired)) != 0
  return (isReachable && !needsConnection)
 }
请参阅此链接:请参阅此链接: