Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check whether device is connected to a VPN in iOS 12

Tags:

ios

swift

vpn

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.

like image 830
Bala Murugan Avatar asked Mar 06 '23 01:03

Bala Murugan


2 Answers

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
}
like image 63
JhonnyTawk Avatar answered Mar 11 '23 15:03

JhonnyTawk


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
like image 36
Mark Thormann Avatar answered Mar 11 '23 15:03

Mark Thormann