Created
April 24, 2018 13:13
-
-
Save azwan082/40241f051982d8d82ec9e763e767d98f to your computer and use it in GitHub Desktop.
Check if iOS app is connected to VPN
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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