iOS VPN Connection Blocks Switching 4G to WiFi Connection

1.6k Views Asked by At

I am creating a VPN connection in Swift with the on demand connect rule below:

        let config = NEVPNProtocolIPSec()
        config.serverAddress = ""
        config.username = ""
        config.passwordReference = ""
        config.authenticationMethod = .sharedSecret
        config.sharedSecretReference = ""
        config.useExtendedAuthentication = true
        config.disconnectOnSleep = true

        let connectRule = NEOnDemandRuleConnect()
        connectRule.interfaceTypeMatch = .any
        vpnManager.onDemandRules = [connectRule]

        vpnManager.protocolConfiguration = config
        vpnManager.localizedDescription = ""
        vpnManager.isOnDemandEnabled = true
        vpnManager.isEnabled = true

This connection works fine. If I am using WiFi it reconnects after disconnecting from WiFi but not vice versa. If am using cellular connection and try to activate WiFi, the phone does not connect to WiFi until I disconnect it from the VPN manually. I believe an active VPN connection blocks switching from 4G to WiFi.

How can I resolve this issue?

1

There are 1 best solutions below

5
On

At the extension, add an observer for defaultPath. Then you will be notified when the interface changes, and you would be able to reconnect with WIFI

Edit: example code

//add observer
let options = NSKeyValueObservingOptions([.new, .old])
self.addObserver(self, forKeyPath: "defaultPath", options: options, context: nil)

//detect interface changes
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
        if let keyPath = keyPath {
            if keyPath == "defaultPath" {
                let oldPath = change?[.oldKey] as! NWPath
                let newPath = change?[.newKey] as! NWPath
                //expensive is 3g, not expensive is wifi
                if !oldPath.isEqual(to: newPath)) {
                  //disconnect the VPN, maybe with cancelTunnelWithError
                }
            }
       }
}