如何在iOS Swift 2中检查WiFi是打开还是关闭? [英] How to check if WiFi is on or off in iOS Swift 2?

查看:75
本文介绍了如何在iOS Swift 2中检查WiFi是打开还是关闭?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查wifi是否已关闭,然后向用户显示警告以检查他/她的连接。

I want to check if the wifi is off then show alert to the user to check his/her connectivity.

我找到这样的代码,但它会检查是否存在是一个互联网连接,而不是检查wifi是打开还是关闭:

I find code like this but it checks if there is an internet connection, not checking if the wifi is on or off:

func isConnectionAvailble()->Bool{

  var rechability = SCNetworkReachabilityCreateWithName(kCFAllocatorDefault, "www.apple.com").takeRetainedValue()

  var flags : SCNetworkReachabilityFlags = 0

  if SCNetworkReachabilityGetFlags(rechability, &flags) == 0
  {
     return false
  }

  let isReachable = (flags & UInt32(kSCNetworkFlagsReachable)) != 0
  let needsConnection = (flags & UInt32(kSCNetworkFlagsConnectionRequired)) != 0
  return (isReachable && !needsConnection)
}


推荐答案

你不能。

使用 Apple的可达性类,你可以根据 NetworkStatus struct来区分三件事: / p>

With Apple's reachability class, you can distinguish three things according to the NetworkStatus struct:

typedef enum : NSInteger {
  NotReachable = 0,         // 1
  ReachableViaWiFi,         // 2
  ReachableViaWWAN          // 3
} NetworkStatus;




  1. 您既没有WiFi也没有移动数据连接。

  2. 您有WiFi连接,但您可能有也可能没有移动数据连接。

  3. 您有移动数据连接,但没有WiFi连接。

无法检查WiFi是否已关闭,或WiFi是否已打开但是附近没有WiFi网络,或者是否已打开飞行模式。

You can't check whether WiFi is turned off, or whether WiFi is turned on but there is no WiFi network nearby, or whether Airplane mode has been turned on.

对于移动数据,您可以使用电话类查找您的设备是否能够进行移动数据连接(iPhone而不是iPad,以及插入的SIM卡),以及您可以检测是否在应用程序的首选项中禁用了移动数据。

For mobile data, you can use the telephony class to find whether your device is capable of mobile data connections (iPhone and not iPad, and SIM card plugged in), and you can detect whether mobile data is disabled in the preferences of your application.

这篇关于如何在iOS Swift 2中检查WiFi是打开还是关闭?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆