How to use NWPathMonitor to assess overall network state?

I can create an instance of NWPathMonitor to monitor a single type of interface (WIFI, cellular, etc) via a labeled init, or I can monitor the overall system (all interfaces) using "init()".


Great! That's what I want to know - is there any interface to the internet that's "satisfiled". The only way to get status is listen for updates by providing a block, that provides a NWPath object. At least one documentation page says that object is hashable (but the Swift interface in Xcode for iOS13 does not say that).


What I **beiieve** i need to do is save all path status' in a dictionary, and when I my block runs, save the state for that particular path (might be cellular, or wifi, or wired, whatever). Then at some point when I need status, I see if any interface is "satisfied" and if not no network.


But I can't save the path in the dictionary - its not hashable! I thought - well, I could use the "debugDescription" as a unique value - shutter shutter!


I have to believe I have misunderstood usage, but I don't find this concept covered in any of relevant blog posts.

Accepted Reply

Turns out the NWPath has an array of interfaces ("availableInterfaces)", they are hashable. So one can keep an array of interfaces and their state.


That said, I did testing with my phone, turning WIFI and Cellular on and off, singly or together, to see when the block gets called. Based on that, I determine:


1) the status of the last block is the overall status (satisfied or not)

2 the last status message of "satisfied" returns the expensive state of the overall networking path: if the system believes it can use WIFI, then its false. If the only option is cellular, then its expensive.


In other words, if you turn cellular on, then wifi, then wifi off, the last status message references cellular and expensive is set.

Replies

Turns out the NWPath has an array of interfaces ("availableInterfaces)", they are hashable. So one can keep an array of interfaces and their state.


That said, I did testing with my phone, turning WIFI and Cellular on and off, singly or together, to see when the block gets called. Based on that, I determine:


1) the status of the last block is the overall status (satisfied or not)

2 the last status message of "satisfied" returns the expensive state of the overall networking path: if the system believes it can use WIFI, then its false. If the only option is cellular, then its expensive.


In other words, if you turn cellular on, then wifi, then wifi off, the last status message references cellular and expensive is set.