NWPathMonitor Failing

I need to check the network connection with NWPathMonitor.

import Foundation
import Network

class NetworkViewModel: ObservableObject {
    let monitor = NWPathMonitor()
    let queue = DispatchQueue(label: "NetworkViewModel")
    @Published var isConnected = false
    
    var connectionDescription: String {
        if isConnected {
            return "You are connected."
        } else {
            return "You are NOT connected."
        }
    }
    
    init() {
        monitor.pathUpdateHandler = { path in
            DispatchQueue.main.async {
                self.isConnected = path.status == .satisfied
            }
        }
        monitor.start(queue: queue)
    }
}
import SwiftUI

struct ContentView: View {
    @StateObject private var networkViewModel = NetworkViewModel()
    
    var body: some View {
        VStack {
        }
        .onAppear {
            if networkViewModel.isConnected {
                print("You are connected.")
            }
            else {
                print("You are NOT connected.")
            }
        }
    }
}

So there is nothing special, not at all. Yet, if I test it with a totally new Xcode project for iOS, it fails and return !isConnected. I've tested it with a macOS application. And it fails. I've tested it with an actual device. It fails. I've tested it with an old project. It still does work. I have no mere idea why new Xcode projects all fail to detect the WiFi connection. This is a total nightmare. Does anybody have a clue? thanks.

Answered by DTS Engineer in 840093022
If they are not connected, I just show some text … never showing the purchase list.

We recommendation against writing code like this. The problem is that NWPathMonitor can generate both false positive and false negatives:

  • The existence of false positives means that your code has to handle the case where you think networking is available but it isn’t. And if you’re gonna write that code anyway, you might as well relying on it all the time.

  • The possibility of false negatives means that you might end up blocking the user from doing something that would’ve otherwise worked.

If, despite the above, I decide to continue down this path then:

  • Test your app in situations where NWPathMonitor returns a false positive. For example, turn off WWAN, then have your device join a Wi-Fi network, and then disconnect the Wi-Fi network from the wider Internet.

  • Use NWPathMonitor in an advisory capacity only. Don’t block user operations based on its result.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Safari does return the web content if I use an iPhone simulator. Yet, NWPathMonitor fails.

Accepted Answer

I didn't notice, but I guess I have to see the status change with onChange(of:initial:_:). Silly me...

I’m glad to hear that you got this sorted.

I do have some feedback about your NetworkViewModel code. On line 6 you create a custom Dispatch queue and only line 23 you use that queue in your path monitor. However, on line 19 you defer all the work to the main queue. That’s kinda roundabout. You could instead remove lines 6, 19 and 21, and then change line 23 to use .main.

I also have a general question: What’s the overall goal of this feature? NWPathMonitor has its uses, but I often see it use incorrectly. For example:

  • It’s reasonable to use it to display a status UI that warns the user that things might not work.

  • And to trigger a retry when something has failed.

  • Don’t use it to prevent the user from attempting to perform a network operation, that is, don’t ‘preflight’ your network connections.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Hello. Thank you for your feedback, The Eskimo. It's well noted. I'll take a deeper look.

I check the network connection with it (NWPathMonitor) so that the users are able to access App Store to make in-app purchases. If they are not connected, I just show some text with a progress guy, never showing the purchase list.

Okay. I've found some time going over your comments, The Eskimo. You are right. The initial lines of code I have shown are unnecessarily redundant. Thank you.

If they are not connected, I just show some text … never showing the purchase list.

We recommendation against writing code like this. The problem is that NWPathMonitor can generate both false positive and false negatives:

  • The existence of false positives means that your code has to handle the case where you think networking is available but it isn’t. And if you’re gonna write that code anyway, you might as well relying on it all the time.

  • The possibility of false negatives means that you might end up blocking the user from doing something that would’ve otherwise worked.

If, despite the above, I decide to continue down this path then:

  • Test your app in situations where NWPathMonitor returns a false positive. For example, turn off WWAN, then have your device join a Wi-Fi network, and then disconnect the Wi-Fi network from the wider Internet.

  • Use NWPathMonitor in an advisory capacity only. Don’t block user operations based on its result.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

I appreciate your kind recommendation. I realize that NWPathMonitor can return unexpected results. That has initiated this question in the first place. In my app, I have employed a measure as to what to do when it ges an unexpected connection result. Yet, I'll see if I need additional measures.

NWPathMonitor Failing
 
 
Q