9

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,

user998753
  • 99
  • 2

2 Answers2

1

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 :)

23r0c001
  • 101
  • 1
  • 6
-3

Check the top answer here. It is for the iOS SDK but the SystemConfigration framework mentioned is available on Mac OSX too.

Community
  • 1
  • 1
Ramon Kleiss
  • 1,584
  • 13
  • 23