How often does system start tunnel for VPN?

I have an iOS app with working network extension for VPN based on `NEPacketTunnelProvider`.

I need to fix some issues in it's behaviour and I can not understand how does system work with extension. Time-to-time I need to check some internal settings of the app to allow or deny connection via VPN.

Documentation says:


Instance Method

startTunnel(options:completionHandler:)

Start the network tunnel.

This method is called by the system to start the network tunnel.


Settings of my VPN turn on on-demand option for it and I have a question: how does often system call this method? Once per session? Often? Is this method called during switching from WiFi to Cellular, or when device returns from Airplane mode?

Replies

The system calls

startTunnel(…)
when it wants to start the tunnel. So:
  • The tunnel must be stopped.

  • There must have been something that has triggered the start. This can be done in a variety of ways:

    • Explicitly by the user using Settings > VPN

    • Explicitly by your app calling

      NETunnelProviderSession.startTunnel(options:)
    • Explicitly by your provider calling

      NEPacketTunnelProvider.cancelTunnelWithError(_:)
      )
    • Implicitly via VPN On Demand.

Once per session?

I don’t know what you mean by “session”.

Often?

That’s too vague to answer.

Is this method called during switching from WiFi to Cellular

That depends on how your VPN provider responds to interface changes. Some providers are tightly bound to the underlying network interface, and thus stop the tunnel (

cancelTunnelWithError(_:)
) when the interface changes. Some providers can rebuild the tunnel over the new interface, and thus the tunnel doesn’t stop in that case.

or when device returns from Airplane mode?

Airplane Mode is very likely to stop the tunnel. When it then starts again when you disable Airplane Mode depends on whether it’s triggered, per my comments above.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"