This post is part of the Local Network Privacy FAQ.
The basic idea is to create an NWConnection for the local network address you’re trying to communicate with. If the connection goes through, you know that you have local network access. If the connection stalls in the .waiting(_:) state, you can look at the current path to see whether it’s waiting for local network access. For example:
You can also use a path update handler to watch for changes:
Back to the FAQ
How do I use the unsatisfied reason property?
The basic idea is to create an NWConnection for the local network address you’re trying to communicate with. If the connection goes through, you know that you have local network access. If the connection stalls in the .waiting(_:) state, you can look at the current path to see whether it’s waiting for local network access. For example:
Code Block connection.stateUpdateHandler = { latestState in switch connection.state { case .waiting(_): if case .localNetworkDenied? = connection.currentPath?.unsatisfiedReason { … no local network access … } … other states … }}
You can also use a path update handler to watch for changes:
Code Block connection.pathUpdateHandler = { latestPath in switch latestPath.status { case .unsatisfied: switch latestPath.unsatisfiedReason { case .localNetworkDenied: … no local network access … … handle other cases … } … handle other statuses … }}
Back to the FAQ