1

我有一个由新的 NetworkExtension 框架创建的带有 VPN 配置的应用程序。它工作得很好,但现在我需要添加一些规则来仅在我尝试连接到特定 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)")
                })
            }
        }
4

2 回答 2

7

我使它符合以下规则:

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]
于 2016-04-12T15:37:53.817 回答
0

您需要添加该行

manager.onDemandEnabled = true
于 2016-04-07T20:29:07.570 回答