I am able to check whether a device is connected to the internet using:
var connected: Bool = true
var zeroAddress = sockaddr_in()
zeroAddress.sin_len = UInt8(sizeofValue(zeroAddress))
zeroAddress.sin_family = sa_family_t(AF_INET)
let defaultRouteReachability = withUnsafePointer(&zeroAddress) {
SCNetworkReachabilityCreateWithAddress(nil, UnsafePointer($0))
}
var flags = SCNetworkReachabilityFlags()
if !SCNetworkReachabilityGetFlags(defaultRouteReachability!, &flags) {
connected = false
}
let isReachable = (flags.rawValue & UInt32(kSCNetworkFlagsReachable)) != 0
let needsConnection = (flags.rawValue & UInt2(kSCNetworkFlagsConnectionRequired)) != 0
if isReachable && !needsConnection && connected {
login(usernameTextField.text!, password: appPassword.text!, environment: environmentUrl)
} else {
let alert = UIAlertView()
alert.title = "No internet connection detected."
alert.addButtonWithTitle("OK")
alert.show()
return
}
But is there any way of checking whether the device is connected to a VPN? The app that I am writing requires a VPN for access, so rather than allowing a user to attempt a login when not connected to a VPN, I would like to prompt them prior (better user experience in my opinion).
Thank you for your help.
ipconfig will tell you whether or not a device is connected to VPN. You would need to know the IP address to look for. route print will let you know if the device is connected to VPN because the routing table will be modified to send some traffic into your VPN.
In order to see whether your VPN is switched on, you can check your IP address before and after connecting to the VPN. If your IP address changes after you have connected to the VPN, then you will know that you have connected properly. You can also run a trace on the IP address to see exactly where the internet thinks you are located.
By querying our API or processing a list of IP addresses, it's very easy to accurately check if an IP addresses belong to a VPN provider. The VPN IP address can also be analyzed for risk analysis, location, and behavior history and similar data.
How to check for IP and/or DNS leaks You need to find out your original IP address given by your ISP. If you are currently using a VPN, turn it off and head to this page. Make a note of your real IP address. Turn on your VPN and go back to the test website. It should now show a different IP address and the country you connected your VPN to.
I made it work for Swift 3/4 by using the following code:
private var isConnectedToVpn: Bool {
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") || key.contains("ipsec") {
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