【问题标题】:iOS make VPN server address variableiOS 使 VPN 服务器地址可变
【发布时间】:2018-02-12 21:38:18
【问题描述】:

我正在为 iOS 创建一个自定义 VPN 应用。我已经有一个简单的 VPN 应用程序,带有开关和连接/断开功能。

我的目标是拥有 VPN 主机变量,因此我可以在应用程序中动态选择相应的服务器,例如,美国一台、法国一台、澳大利亚一台。

我尝试将协议配置服务器地址最初设置为“无”,然后根据我的连接方法中的选择进行相应更改:

func connect() {
    NEVPNManager.shared().loadFromPreferences(completionHandler: { error in
        guard error == nil else {
            NSLog("Failed loading preferences: \(error!.localizedDescription)")
            return
        }

        if (NEVPNManager.shared().protocolConfiguration == nil) {
            NSLog("Not installed")
            return
        }

        NSLog("serveraddress: \(String(describing: NEVPNManager.shared().protocolConfiguration?.serverAddress))")
        NEVPNManager.shared().protocolConfiguration?.serverAddress = "xxxxxxx"
        NSLog("serveraddress: \(String(describing: NEVPNManager.shared().protocolConfiguration?.serverAddress))")

        do {
            let options: [String: NSObject] = [
                NEVPNConnectionStartOptionUsername : "xxxxxx" as NSObject,
                NEVPNConnectionStartOptionPassword : "xxxxxx" as NSObject
            ]
            try NEVPNManager.shared().connection.startVPNTunnel(options: options)
            return
        } catch let error as NSError {
            NSLog("Failed to start vpn: \(error)")
            return
        }
    })
}

这不起作用,VPN 连接状态卡在“正在连接”中。

所以我的问题是如何使服务器地址可变? 还有什么是远程标识符?

【问题讨论】:

  • 您的应用程序订阅了NSNotification.Name.NEVPNStatusDidChange 通知吗?
  • 是的,NEVPNStatusDidChange 通知有效。我的问题是为什么我在 loadFromPreferences 上下文中设置服务器地址时无法连接,但是当我在初始设置中设置它以请求访问 VPN 设置时却可以。

标签: ios iphone swift vpn


【解决方案1】:

在我们的产品中,我们使用配置文件(json/plist/txt),当扩展进行连接调用时,自动连接选定的服务器!

【讨论】:

    猜你喜欢
    • 2015-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-01
    • 1970-01-01
    • 2015-12-13
    • 2014-06-27
    相关资源
    最近更新 更多