我仅在第一次连接到 VPN 服务器时收到“Error Domain=NEVPNErrorDomain Code=1\"(null)\"”,之后就可以正常工作了。
我检查过NEVPNErrorDomain Error 1 when trying to start TunnelProvider network extension与我的问题类似,并按照此解决方案中所述的方式进行操作,但仍然遇到相同的错误。
这是我使用网络扩展连接到 VPN 服务器的代码。
func initVPNTunnelProviderManager(){
self.vpnManager.loadFromPreferences { (error) -> Void in
if((error) != nil) {
print("VPN Preferences error: 1")
}
else {
let p = NEVPNProtocolIKEv2()
p.username = "******"
p.remoteIdentifier = "*****"
p.serverAddress = "******"
let keychain = KeychainSwift()
let data = keychain.getData("vpnPassword")
p.passwordReference = data
p.authenticationMethod = NEVPNIKEAuthenticationMethod.none
p.useExtendedAuthentication = true
p.disconnectOnSleep = false
self.vpnManager.protocolConfiguration = p
self.vpnManager.isEnabled = true
self.vpnManager.saveToPreferences(completionHandler: { (error) -> Void in
if((error) != nil) {
print("VPN Preferences error: 2")
}
else {
var startError: NSError?
do {
try self.vpnManager.connection.startVPNTunnel()
}
catch let error as NSError {
startError = error
print(startError)
}
catch {
print("Fatal Error")
fatalError()
}
if((startError) != nil) {
**// Here it comes first time later onwards it goes to else block. (Which I'm expecting first time too)
print("VPN Preferences error: 3")**
let alertController = UIAlertController(title: "Oops..", message:
"Something went wrong while connecting to the VPN. Please try again.", preferredStyle: UIAlertControllerStyle.alert)
alertController.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.default,handler: nil))
self.present(alertController, animated: true, completion: nil)
print(startError)
}
else {
print("Start VPN")
}
}
})
}
}
}
有人可以帮我解释为什么这是第一次进去吗?
if((startError) != nil) {
// Here is control coming for first time
}
else {
// Which should always come here //later onwards its coming here, not first time.
}
注意:我找到了相关帖子 here on GitHub但如果我尝试这个解决方案,它会给出Optional(Error Domain=NEVPNErrorDomain Code=4 "(null)") 问题,因为配置已过时,需要加载。您应该调用 loadFromPreferencesWithCompletionHandler: 并在完成处理程序中修改要修改的值,然后调用 saveToPreferencesWithCompletionHandler:。
提前致谢。
最佳答案
解决方案是:
在 saveToPreferences 之后再次调用 loadFromPreferences 并在加载完成后启动连接将解决该问题。这听起来很愚蠢。
self.vpnManager.saveToPreferences(completionHandler: { (error) -> Void in
if ((error) != nil) {
print("VPN Preferences error: 2")
}
else {
self.vpnManager.loadFromPreferences(completionHandler: { (error) in
if ((error) != nil) {
print("VPN Preferences error: 2")
}
else {
var startError: NSError?
do {
try self.vpnManager.connection.startVPNTunnel()
}
catch let error as NSError {
startError = error
print(startError)
}
catch {
print("Fatal Error")
fatalError()
}
if ((startError) != nil) {
print("VPN Preferences error: 3")
let alertController = UIAlertController( title: "Oops..", message: "Something went wrong while connecting to the VPN. Please try again.", preferredStyle: UIAlertControllerStyle.alert)
alertController.addAction( UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.default,handler: nil))
self.present(alertController, animated: true, completion: nil)
print(startError)
}
else {
self.VPNStatusDidChange(nil)
print("Start VPN")
}
}
})
}
})
关于ios - "Error Domain=NEVPNErrorDomain Code=1\"(null)\""连接 VPN 服务器时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47550706/