I am using the code below (Swift 3 and Swift 4 compatible) to check the VPN connection on iOS devices which is not working in iOS 12 and above. How can I check the vpn connectivity in iOS 12
func isVPNConnected() -> Bool {
let cfDict = CFNetworkCopySystemProxySettings()
let nsDict = cfDict!.takeRetainedValue() as NSDictionary
let keys = nsDict["__SCOPED__"] as! NSDictionary
for key: String in keys.allKeys as! [String] {
if (key == "tap" || key == "tun" || key == "ppp" || key == "ipsec" || key == "ipsec0") {
return true
}
}
return false
}
Thank you for your help.
The list of keys has been changed since iOS 12 and iOS 13
2 keys have been added
utun1 and utun2
so the function should be:
static func isConnectedToVPN() -> Bool {
let cfDict = CFNetworkCopySystemProxySettings()
let nsDict = cfDict!.takeRetainedValue() as NSDictionary
let keys = nsDict["__SCOPED__"] as! NSDictionary
for key: String in keys.allKeys as! [String] {
if (key == "tap" || key == "tun" || key == "ppp" || key == "ipsec" || key == "ipsec0" || key == "utun1" || key == "utun2") {
return true
}
}
return false
}
Trying adding the key 'utun1' to your check (or prefixed with 'utun' followed by a number).
for key: String in keys.allKeys as! [String] {
if (key == "tap" || key == "tun" || key == "ppp" || key == "ipsec" || key == "ipsec0" || key == "utun1") {
return true
}
}
return false
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