NetworkExtension connectOnDemand 规则不起作用

NetworkExtension connectOnDemand rules doesn't work

我有一个应用程序,其 VPN 配置由新的 NetworkExtension 框架创建。它工作得很好,但现在我需要添加一些规则来仅在我尝试连接到特定 URL 时才打开此 VPN。我计划使用 NEVPNManager 的 connectOnDemand 功能,但它似乎对我不起作用。当我在 safari 中打开 http://some-site.com 时,我的 VPN 连接应该建立,但由于某种原因它没有。我尝试了不同类型的配置以及使用生成的 .mobileconfig 文件来使 connectOnDemand 工作,但没有成功。它出什么问题了? 我正在测试这样的代码:

let manager = NEVPNManager.sharedManager()

manager.enabled = true

manager.loadFromPreferencesWithCompletionHandler { (err) -> Void in
    manager.removeFromPreferencesWithCompletionHandler { (err0) -> Void in
                print("err0 \(err0)")
                print("err \(err)")
                let config = NEVPNProtocolIPSec()
                config.localIdentifier = "NEVPNProtocolIPSec"
                config.remoteIdentifier = "NEVPNProtocolIPSecRemote"
                config.disconnectOnSleep = true
                config.serverAddress = server
                config.authenticationMethod = .Certificate
                //configurating here
                manager.protocolConfiguration = config
                let onDemandRule1 = NEOnDemandRuleConnect()
                onDemandRule1.DNSSearchDomainMatch = ["some-site.com", "*.some-site.com"]

                manager.onDemandRules = [onDemandRule1]
                manager.onDemandEnabled = true
                manager.saveToPreferencesWithCompletionHandler({ (err2) -> Void in
                    print("err2 \(err2)")
                })
            }
        }

您需要添加行

manager.onDemandEnabled = true

我让它与下一个规则一起工作:

let onDemandRule = NEOnDemandRuleEvaluateConnection()
let evaluateRule = NEEvaluateConnectionRule(matchDomains: ["*.some-site.com"], andAction: .ConnectIfNeeded)
evaluateRule.probeURL = NSURL(string: "https://a.url.accecable.only.from.vpn")

onDemandRule.connectionRules = [evaluateRule]
manager.protocolConfiguration = config
manager.onDemandRules = [onDemandRule]