【发布时间】:2017-02-14 11:18:18
【问题描述】:
伙计们。
我正在尝试建立与我的服务器的 VPN 连接,但出现了问题。
我试过手动设置,效果不错。
这是我的配置代码。服务器对象肯定有一个名称和一个正确的主机,我尝试禁用扩展身份验证。 Afaik 远程和本地标识符只是随机字符串,所以我不知道我做错了什么。
fileprivate func updateCurrentVPN(withServer server: Server, completion: ((Bool) -> Void)?) {
let passwordRef = KeychainWrapper.standard.dataRef(forKey: self.account.username)
let secretRef = KeychainWrapper.standard.dataRef(forKey: API.ipsecSecretKey())
// checking if the strings are correct in a debugger
let password = KeychainWrapper.standard.string(forKey: self.account.username)
let secret = KeychainWrapper.standard.string(forKey: API.ipsecSecretKey())
// VPN
let manager = NEVPNManager.shared()
manager.loadFromPreferences { (error) in
if (error != nil)
{
print("Error: ", error.debugDescription)
completion?(false)
}
else
{
let prtcl = NEVPNProtocolIPSec()
prtcl.username = self.account.username
prtcl.passwordReference = passwordRef
prtcl.serverAddress = server.host
prtcl.authenticationMethod = NEVPNIKEAuthenticationMethod.sharedSecret
prtcl.sharedSecretReference = secretRef
prtcl.localIdentifier = "Test iOS device"
prtcl.remoteIdentifier = server.name
prtcl.useExtendedAuthentication = true
prtcl.disconnectOnSleep = false
manager.isEnabled = true
manager.protocolConfiguration = prtcl
manager.isOnDemandEnabled = false
manager.localizedDescription = "MyVPN Configuration"
manager.saveToPreferences(completionHandler: { (error) in
if (error != nil)
{
print("Error: ", error.debugDescription)
completion?(false)
}
else
{
self.serverButton.setTitle(server.name, for: UIControlState.normal)
print("VPN prefs saved")
completion?(true)
}
})
}
}
配置保存后我会这样做:
let manager = NEVPNManager.shared()
do {
try manager.connection.startVPNTunnel()
} catch {
print(error.localizedDescription)
connectButton.setTitle("da failure :(", for: UIControlState.normal)
}
但它从不抛出错误,只是在系统警报中提示我“与 VPN 服务器的协商失败”。 顺便说一句,是否有可能捕捉到这种错误?
【问题讨论】:
标签: ios swift swift3 networkextension nevpnmanager