Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS VPN Connection Blocks Switching 4G to WiFi Connection

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?

like image 657
mTuran Avatar asked May 16 '17 20:05

mTuran


People also ask

Why is my VPN blocking my internet iPhone?

If you have a VPN App or profiles installed, or a security App such as Norton, these may be interfering with DHCP and preventing your device from obtaining a valid IP Address for your iPhone hotspot - or the WiFi network to which you are attempting to connect.

Why does my VPN interfere with my WiFi?

If you have let your VPN service use the default gateway of the remote network, it may cause Internet connectivity issues. Such configuration cancels what has been specified in your TCP/IP as the default gateway setting.

Does VPN not work on 4G?

Does a VPN work on cellular data? Yes, a VPN works on cellular data in very much the same way it works on WiFi. Simply turn on your VPN while connected to 3G or 4G and it'll encrypt your web traffic and hide your IP address.

Does VPN affect WiFi on iPhone?

VPN can affect WiFi, but it's worth it Your personal traffic will be encrypted and your IP will be masked. Sure, it might be a bit slower than staying connected without any extra security layers, but the differences shouldn't be huge.


1 Answers

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
                }
            }
       }
}
like image 182
Roee84 Avatar answered Sep 21 '22 12:09

Roee84