Skip to content

Instantly share code, notes, and snippets.

@azwan082
Created April 24, 2018 13:13
Show Gist options
  • Save azwan082/40241f051982d8d82ec9e763e767d98f to your computer and use it in GitHub Desktop.
Save azwan082/40241f051982d8d82ec9e763e767d98f to your computer and use it in GitHub Desktop.
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