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?
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.
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 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.
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.
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
}
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With