【问题标题】:Configuring ios8 VPN configuration and control in Swift在 Swift 中配置 ios8 VPN 配置和控制
【发布时间】:2025-12-01 11:05:01
【问题描述】:

我正在尝试使用 ios8 的 VPN 配置和控制设置应用内 VPN 连接,但我被卡住了。我正在使用http://ramezanpour.net/post/2014/08/03/configure-and-manage-vpn-connections-programmatically-in-ios-8/ 的资源来提供帮助,但我在将协议绑定到 VPN 管理器时遇到问题。

到目前为止,我有以下代码,根据上面的教程,您需要将协议绑定到管理器...

func initVPN() {
    let manager: NEVPNManager = NEVPNManager.sharedManager()
    let p = NEVPNProtocolIPSec()

    p.username = "ospencer"
    p.passwordReference = getPasscodeNSData("vpnPassword")
    p.serverAddress = "vconnect.uk.capgemini.com"
    p.authenticationMethod = NEVPNIKEAuthenticationMethod.SharedSecret
    p.sharedSecretReference = getPasscodeNSData("vpnSharedSecret")
    p.useExtendedAuthentication = true
    p.disconnectOnSleep = false

我尝试编写 manager.protocol = p,但我收到错误“预期的成员名称如下。”和“协议声明中的预期标识符”。我尝试了在一些 Objective C 示例“[manager setProtocol:p]”中使用的 setProtocol 函数,但在最新的 xcode 构建中似乎不存在。

有人可以帮忙吗?此外,几乎没有关于在互联网上使用 VPN 功能的详细信息——即使在 Apple 的网站上也是如此。任何指针都会很有用。

谢谢,

奥利弗。

【问题讨论】:

标签: ios swift vpn


【解决方案1】:
To use a reserved word as an identifier, put a backtick (`) before and after it. For example, class is not a valid identifier, but

class 有效。反引号不被视为 标识符; x 和 x 含义相同。

https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html

【讨论】: