Correct Usage of NEPacketTunnelProvider’s cancelTunnelWithError()

Hi everyone,

I’ve been working with the NEPacketTunnelProvider class and came across the cancelTunnelWithError() method. The documentation mentions its general purpose but doesn’t provide much clarity on how and when it should be called.

From what I’ve gathered in other forum posts, it seems that cancelTunnelWithError() should be called within my own implementation of the stopTunnel() method, but I’m not entirely sure if that’s the correct usage or whether there are specific scenarios where this applies.

Here are my specific questions:

  1. Is it correct to always call cancelTunnelWithError() in my implementation of stopTunnel()?
  2. Are there specific conditions or scenarios where cancelTunnelWithError() is the preferred way to terminate a tunnel session, rather than other termination methods?
  3. What does the system do with the error that I pass to cancelTunnelWithError()? Does it have an impact on how the session termination is handled?
  4. Are there best practices or common pitfalls to avoid when using cancelTunnelWithError()?

Any insights, examples, or guidance would be greatly appreciated!

Thanks in advance for your help!

it seems that cancelTunnelWithError() should be called within my own implementation of the stopTunnel() method

No, that’s not right. A tunnel can stop for one of two reasons:

  • From above, that is, something in the system decided to stop the tunnel. In that case the system calls stopTunnel().

  • From below, that is, something on the network caused the tunnel to fail. In that case your provider should call cancelTunnelWithError().

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Correct Usage of NEPacketTunnelProvider’s cancelTunnelWithError()
 
 
Q