is there any system event which I can use in my program to detect a network change on macOS? I would like to call a function if the network state change, without having a lookup every x seconds in my program. Currently I'm using 'scheduledTimer' to make a lookup...
Is there a better solution for that?
This is achievable with the
NWPathMonitorAPI. Using it, we can have a function called each time the state of the network changes (i.e., every time an interface connects or disconnects).Compiling and running that will emit lines that look like:
You can use the fields on the
pathobject to detect the currently available interfaces, and check their connectivity and features (ethernet vs wifi, etc).