On Mac OS X, is there a way to get asynchronous notification when the network interface (en0, en1) status changes, such as network interface is active, inactive etc?
Instead of polling for the status, i would like to know is there a way to get asynchronous notification, so that whenever there is a change, notification is fired and app can handle it.
Is this possible?
Thanks,
It depends on exactly what changes in the network state you want to be informed of (e.g. do you just want to know when you gain or lose a network connection, or do you want to know things like if you've switched WiFi networks or some more esoteric detail like that), and if you provide more details about that I can provide a better answer, but generally speaking I reckon you want to use the SystemConfiguration
framework, specifically probably the SystemConfiguration/SCNetworkReachability.h
APIs (described in Apple docs here).
Even more specifically than that, I think SCNetworkReachabilityCallBack
is the type of callback function you want to use. This here is the Apple documentation for it.
Finally, to set up that callback, I mean first you have to write your callback function, but to "register" it so SystemConfiguration will use it to call you back when your network status changes, use this guy: SCNetworkReachabilitySetCallback(_:_:_:)
described in official Apple documentation here.
Hope this helps and if you want more specific help just add some more specifics to your question :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With