Stop NEPacketTunnel when internet is switched off

Hi,

We have configued VPN using NEPacketTunnelProvider. When user switch off the internet, VPN is stll connected. We want VPN also to disconnect once internet if off. Is there a way to achieve this?

Thanks

Accepted Reply

I found the fix. Use NWPathMonitor and in case no network event, called cancelTunnelWithError

Replies

There are a few things you could take a look at here; first you could setup an observer on the NEVPNStatusDidChangeNotification. If you are detecting the VPN disconnection changes there when the internet is switched off, you should be able to manually disconnection there. The second is to look through the NEOnDemandRuleConnect and NEOnDemandRuleDisconnect rules and see if these make sense to setup VPN connect ONLY when scenarios in your VPN.



Matt Eaton

DTS Engineering, CoreOS

meaton3 at apple.com

If the VPN is connected, but the containing app is at the background, would the observer still detect the VPN status changes? Would it be possible to manually disconnect the VPN when the containing app isn't on foreground?

No, in this scenario, the host app would not get these notifications.


Matt Eaton

DTS Engineering, CoreOS

meaton3 at apple.com

As roee84 pointed out. My scenario is VPN is already cinfigued and running. Containing app is no longer running in background (as iOS kill background apps). Is there a way in NEPacketTunnelExtension itself, where it checks for internet and disables?


I have observer registered but it does not recieve the call back becuase app is not running.


Strangely, when Wifi is off, I cannot toggle VPN to ON state (manually). But when VPN is already ON then I can switch off wifi. VPN remains connected. This causing issues with other apps.

I found the fix. Use NWPathMonitor and in case no network event, called cancelTunnelWithError