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

编程入门 行业动态 更新时间:2024-10-27 05:36:25
如何在iOS Swift 2中检查WiFi是打开还是关闭?(How to check if WiFi is on or off in iOS Swift 2?)

我想检查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.

更多推荐

本文发布于:2023-08-04 18:44:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1420722.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何在   iOS   Swift   WiFi   check

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!