【问题标题】:Check whether device is connected to a VPN in iOS 12在 iOS 12 中检查设备是否连接到 VPN
【发布时间】:2019-04-18 12:39:30
【问题描述】:

我正在使用以下代码(兼容 Swift 3 和 Swift 4)来检查在 iOS 12 及更高版本中无法运行的 iOS 设备上的 VPN 连接。如何在 iOS 12 中检查 vpn 连接

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
}

感谢您的帮助。

【问题讨论】:

  • 你遇到了什么问题,我的意思是你验证密钥在子字符串中不可用
  • 我的问题是我需要检查设备是否连接到 vpn 或。不是。以上代码在 iOS 11 中运行良好,但在 iOS 12 中总是返回 false。如何检查最新 iOS 中的 vpn 连接?
  • 您是否验证了 [string] 中的键返回的内容
  • 是的,总是得到“en0”
  • 我想你尝试过在同一个 IP 地址

标签: ios swift vpn


【解决方案1】:

自 iOS 12 和 iOS 13 以来,按键列表已更改

已添加 2 个键

utun1 和 utun2

所以函数应该是:

static func isConnectedToVPN() -> 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" || key == "utun1" || key == "utun2") {
               return true
           }
       }
       return false
}

【讨论】:

    【解决方案2】:

    尝试将密钥“utun1”添加到您的支票中(或以“utun”为前缀,后跟一个数字)。

    for key: String in keys.allKeys as! [String] {
        if (key == "tap" || key == "tun" || key == "ppp" || key == "ipsec" || key == "ipsec0" || key == "utun1") {
            return true
        }
    }
    return false
    

    【讨论】:

      【解决方案3】:

      在我的情况下,我收到了 ipsec4 并且应用程序无法识别 VPN。 因此我想出了一些不同的东西:

      struct VpnChecker {
      
          private static let vpnProtocolsKeysIdentifiers = [
              "tap", "tun", "ppp", "ipsec", "utun"
          ]
      
          static func isVpnActive() -> Bool {
              guard let cfDict = CFNetworkCopySystemProxySettings() else { return false }
              let nsDict = cfDict.takeRetainedValue() as NSDictionary
              guard let keys = nsDict["__SCOPED__"] as? NSDictionary,
                  let allKeys = keys.allKeys as? [String] else { return false }
      
              // Checking for tunneling protocols in the keys
              for key in allKeys {
                  for protocolId in vpnProtocolsKeysIdentifiers
                      where key.starts(with: protocolId) {
                      // I use start(with:), so I can cover also `ipsec4`, `ppp0`, `utun0` etc...
                      return true
                  }
              }
              return false
          }
      }
      
      

      用法:VpnChecker.isVpnActive()

      我还写了一篇关于它的小博文here

      【讨论】:

      • 它在 ios 14.5 中不起作用..任何帮助?
      猜你喜欢
      • 2013-05-05
      • 1970-01-01
      • 1970-01-01
      • 2011-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-09
      • 1970-01-01
      相关资源
      最近更新 更多