Hi! I am trying to use the NWPathMonitor to check if wifi is on or off. However the NWPath object from the monitor always has unsatisfiedReason property being "notAvailable" regardless whether I turn the wifi off or on (unsatisfiedReason doesn't change even if iPhone is connected to wifi, with NWPath.state satisfied..). Below is the simple code I tried.
Can you help me to find the solution pls?
Thank you!
private var monitor = NWPathMonitor(requiredInterfaceType: .wifi)
init() {
monitor.pathUpdateHandler = { [weak self] path in
if #available(iOS 14.2, *) {
print(path.status)
print(path.unsatisfiedReason)//always notAvailable
if path.status == .satisfied {
print("Connected to internet")
print("Wifi is on")
} else {
print("No connection")
if path.unsatisfiedReason == .wifiDenied {
print("Wifi is off") // never execute !
} else {
print("Wifi is on")
}
}
}
}
monitor.start(queue: .global())
}
deinit {
monitor.cancel()
}
I suspect you’re confused about what the .wifiDenied status means. This isn’t related to whether the Wi-Fi is on or off, but rather whether your app has access to Wi-Fi. You’ll never see this on a standard iPhone because that has no way for the user to deny your app access to Wi-Fi; in Settings you can only deny apps access to WWAN.
To see this status you have to run on an iPhone bought in China. On such a device the Settings app has three options:
-
Off — No network access
-
WLAN — Wi-Fi only
-
WLAN & Cellular Data — Wi-Fi and WWAN
If you select Off, you’ll get the .wifiDenied status.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"