Search code examples
iosios9vpnnetworkextension

startVPNTunnel not throws errors


I'm calling startVPNTunnel() and then in the PacketTunnelProvider class, I execute the completionBlock with an error. The call to startVPNTunnel() is inside a try-catch block, but it's seems that there is nothing to catch. I just want to alert the user if the connection succeeded or not. Anyone else was able to catch those errors?

The relevant code is very simple:

do {
       try vpnManager!.connection.startVPNTunnel()
   }
catch {
       NSLog("roee: Failed to start vpn: \(error)")
}

And inside PacketTunnelProvider:

 let error = NSError(domain: NEVPNErrorDomain, code: NEVPNError.ConfigurationInvalid.rawValue, userInfo: nil)
            PacketTunnelProvider.pendingStartCompletionHandler!(error)
            PacketTunnelProvider.pendingStartCompletionHandler = nil

Solution

  • I believe startVPNTunnel talks to OS and let it knows that VPN needs to be started.

    OS will do couple of checks around VPN configuration and will immediately return positive result (if configuration is fine) or error (if something is wrong). And if an error is returned then this API will throw.

    However, your Network extension will be started after startVPNTunnel returned.

    So you can't communicate like that.

    You can register for NSNotification.Name.NEVPNStatusDidChange to see VPN status changes.