How to check if there is an active network on macOS

Hello,

I'm developing an application with NEAppProxyProvider.

Everything works flawlessly. I see my VPN network interface in the System Preferences -> Network list and it successfully goes to state Connected.

But there is a problem when there is no active network, e.g. user's wifi or LAN connection is disconnected. Then my VPN does not launch/connect. Yes, the user can than manually connect the VPN from the System Preferences -> Network when he connects to a network. But i'd like launch/connect the VPN automatically if there is any active network. Because the user might be traveling with his MacBook between different wifi ACs, or there might be a network outage, etc. And I don't want to annoy the user with the need of manual launch/connect of the VPN.

Is there any way to set a callback when an active network becomes available? It would largely increase user experience of my application.
Answered by Systems Engineer in 650029022

Is there any way to set a callback when an active network becomes available? It would largely increase user experience of my application.

If you are using NEAppProxyProviderManager you can look at using either a NEAppRule or a NEOnDemandRule. Using a NEOnDemandRule you could do a match for NEOnDemandRuleInterfaceType on NEOnDemandRuleInterfaceTypeAny. If using NEAppRule you could connect when Safari is accessing a domain. For example, when Safari connects to example.com this would trigger the NEAppProxyProviderManager to startVPNTunnel.

Code Block swift
let manager = NEAppProxyProviderManager()
...
var appRules = [NEAppRule]()
let appRule = NEAppRule(signingIdentifier: "com.apple.Safari", designatedRequirement: "identifier \"com.apple.Safari\" and anchor apple")
appRule.matchDomains = ["example.com"]
appRules.append(appRule)
manager.appRules = appRules
manager.isOnDemandEnabled = true


Matt Eaton
DTS Engineering, CoreOS
meaton3@apple.com
Accepted Answer

Is there any way to set a callback when an active network becomes available? It would largely increase user experience of my application.

If you are using NEAppProxyProviderManager you can look at using either a NEAppRule or a NEOnDemandRule. Using a NEOnDemandRule you could do a match for NEOnDemandRuleInterfaceType on NEOnDemandRuleInterfaceTypeAny. If using NEAppRule you could connect when Safari is accessing a domain. For example, when Safari connects to example.com this would trigger the NEAppProxyProviderManager to startVPNTunnel.

Code Block swift
let manager = NEAppProxyProviderManager()
...
var appRules = [NEAppRule]()
let appRule = NEAppRule(signingIdentifier: "com.apple.Safari", designatedRequirement: "identifier \"com.apple.Safari\" and anchor apple")
appRule.matchDomains = ["example.com"]
appRules.append(appRule)
manager.appRules = appRules
manager.isOnDemandEnabled = true


Matt Eaton
DTS Engineering, CoreOS
meaton3@apple.com
Matt, Thank you very much for your reply. That looks really promising. I'm using NETransparentProxyManager so I believe that I fill the .onDemandRules instead of the .appRules in the case of your example. Is it possible to write a rule that connects the VPN on any traffic?
How to check if there is an active network on macOS
 
 
Q