Detect VPN / Personal Hotspot

Is there any reliable way to check for an active VPN connection or a connection over Personal Hotspot?

I am currently building an app that uses MultipeerConnectivity for peer-to-peer communication and from what I have learned so far MPC does not work with VPN / Personal Hotspot turned on.

To provide a first-class user experience (and tackle VPN problems in App Review) I want to check for a VPN / Personal Hotspot connection and display the user an alert encouraging him to disable the corresponding connection.
Just like AirDrop which should work quite similar to MPC.

I found this code snippet on Stackoverflow but since I do not really now anything about network interfaces in iOS I came here to seek help. :)

Code Block
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
}


This is essentially the same as I use in my app. Although the keys could be slightly different, for example "tun0", "tun1" etc. So instead of exact matches my if statement looks like this:

Code Block
if key.contains("tap") OR key.contains("tun") OR key.contains("ppp") OR key.contains("ipsec") {
  return true
}


(replace OR with ||, seems that the code formatting on the forums likes to replace them with underscores?)
Thanks for your answer @maxxfrazer!

Did your app pass app review with that code?

And is there any chance that you know what network interfaces are being used for VPN / personal hotspot in iOS?
@Maci Yes it finally got through, I was basically displaying a small toast message, warning that using a VPN may interfere with MultipeerConnectivity interfaces when I noticed it was switched on, and then the reviewer approved. No confirmation that this was indeed the fix, but I can assume it was.

I left warning messages rather than just leaving a blanket stop because some site-access VPNs are okay, it's only specific site-to-site VPNs that divert all network communications, and I do not know how to tell the differences between them.
Detect VPN / Personal Hotspot
 
 
Q