Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Check if iOS app is connected to VPN
func isConnectedToVpn() -> Bool {
let host = "www.example.com"
guard let reachability = SCNetworkReachabilityCreateWithName(nil, host) else {
return false
}
var flags = SCNetworkReachabilityFlags()
if SCNetworkReachabilityGetFlags(reachability, &flags) == false {
return false
}
let isOnline = flags.contains(.reachable) && !flags.contains(.connectionRequired)
if !isOnline {
return false
}
let isMobileNetwork = flags.contains(.isWWAN)
let isTransientConnection = flags.contains(.transientConnection)
if isMobileNetwork {
if let settings = CFNetworkCopySystemProxySettings()?.takeRetainedValue() as? Dictionary<String, Any>,
let scopes = settings["__SCOPED__"] as? [String:Any] {
for (key, _) in scopes {
if key.contains("tap") || key.contains("tun") || key.contains("ppp") {
return true
}
}
}
return false
} else {
return isTransientConnection
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment