0

We are converting a mobile application from iOS native (Swift) to Xamarin.iOS (so it can eventually be deployed to multiple operating systems).

I am trying to research how to do the following in Xamarin.iOS: 1 - Check if a VPN connection is active/enabled on the iOS device 2 - Bring up the VPN settings screen (or better, enable a specific VPN automatically)

For: 1 - Is this https://learn.microsoft.com/en-us/dotnet/api/networkextension.nevpnstatus applicable? 2 - Same for https://learn.microsoft.com/en-us/dotnet/api/networkextension.netunnelprovidermanager?

Snippets from existing Swift code:

func connectToVpn(){ //mention the connection name instead of exposing the server //let connectUrl = URL(string: "mobileconnect://connect?name=*********") let connectUrl = URL(string: "mobileconnect://connect?") if UIApplication.shared.canOpenURL(connectUrl!) == true { UIApplication.shared.openURL(connectUrl!) } } var isVpnConnected : Bool { let dict = CFNetworkCopySystemProxySettings()?.takeUnretainedValue() as? [String: AnyObject] guard let keys = dict?["__SCOPED__"]?.allKeys as? [String] else{ return false } for key in keys { if key.contains("tap") || key.contains("tun") || key.contains("ppp") { return true } } return false } Any comments/suggestions/youtube video/blog entry links would be greatly appreciated.

Edit: For 1 - Realized I don't need to check the VPN status, will just ping an internal server/host to see if it responds.

rwolters3
  • 63
  • 1
  • 7

2 Answers2

0

iOS Only allows you to open your own app's settings page from within your app. If your app does not implement its own settings page, then the main settings page will open instead, but you can not open a specific system settings page. See this discussion elsewhere on SO: How to open Settings programmatically like in Facebook app?

jgoldberger - MSFT
  • 5,978
  • 2
  • 20
  • 44
0

Your approach is the way to go, that way it doesn't matter what interface type is used. Will note that there are other interface name such as "utun2". Not sure where to pull all the interface names from but here's an updated Xamarin version in case it helps someone out.

public bool isVPNConnected()
{
    var settings = CoreFoundation.CFNetwork.GetSystemProxySettings();
    var keys = settings.Dictionary.ValueForKey(new NSString("__SCOPED__")) as NSDictionary;

    return keys.Any(k => new string[] { "tap", "tun", "utun2", "ppp", "ipsec", "ipsec0" }.Contains(k.Key.ToString()));
}
DannyC
  • 401
  • 4
  • 15