我想检查wifi是否关闭,然后向用户显示提醒以检查他/她的连接。
我找到这样的代码,但它会检查是否有互联网连接,而不是检查wifi是打开还是关闭:
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) }I want to check if the wifi is off then show alert to the user to check his/her connectivity.
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结构区分三件事:
typedef enum : NSInteger { NotReachable = 0, // 1 ReachableViaWiFi, // 2 ReachableViaWWAN // 3 } NetworkStatus; 您既没有WiFi也没有移动数据连接。 您有WiFi连接,但您可能有也可能没有移动数据连接。 您有移动数据连接,但没有WiFi连接。您无法检查WiFi是否已关闭,或WiFi是否已打开但附近没有WiFi网络,或者是否已打开飞行模式。
对于移动数据,您可以使用电话类查找您的设备是否能够进行移动数据连接(iPhone而不是iPad,以及插入的SIM卡),并且您可以检测是否在应用程序的首选项中禁用了移动数据。
You can't.
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; You have neither WiFi nor mobile data connection. You have a WiFi connection, but you may or may not have a mobile data connection. You have a mobile data connection, but no WiFi connection.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.
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.
更多推荐
发布评论