问题描述
在连接到VPN服务器时,我第一次仅收到错误域= NEVPNErrorDomain代码= 1 \"(空)\"",以后再正常使用即可.
I'm getting "Error Domain=NEVPNErrorDomain Code=1 \"(null)\"" only first time while connecting to VPN server, later onwards it works perfectly.
我在尝试时已检查 NEVPNErrorDomain错误1启动与我的问题类似的TunnelProvider网络扩展,并按照其在此解决方案中所说的方法进行操作,但仍然出现相同的错误.
I've checked NEVPNErrorDomain Error 1 when trying to start TunnelProvider network extension similar to my problem and followed the way its said in this solution but still getting same error.
这是我的使用网络扩展连接到VPN Server的代码.
So here is my code for connecting to VPN Server using Network Extension.
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")
}
}
})
}
}
}
有人可以帮我为什么第一次进入吗?
Can someone help me why its first time going inside if ?
if((startError) != nil) {
// Here is control coming for first time
}
else {
// Which should always come here //later onwards its coming here, not first time.
}
注意:我在GitHub上找到了相关的帖子此处,但是如果我尝试此解决方案,给Optional(Error Domain = NEVPNErrorDomain Code = 4(null)")问题,因为,配置是陈旧的,需要加载.您应该调用loadFromPreferencesWithCompletionHandler :,然后在完成处理程序中修改要修改的值,然后调用saveToPreferencesWithCompletionHandler:.
NOTE : I found relevant post here on GitHub but if I try this solution its giving Optional(Error Domain=NEVPNErrorDomain Code=4 "(null)") issue because, The configuration is stale and needs to be loaded. You should call loadFromPreferencesWithCompletionHandler: and in the completion handler modify the values you want to modify, and then call saveToPreferencesWithCompletionHandler:.
谢谢.
推荐答案
解决方案是:
紧随 saveToPreferences 之后再次调用 loadFromPreferences ,并在加载完成后启动连接将解决此问题.听起来很蠢.
Calling loadFromPreferences again right after saveToPreferences and starting the connection after load completion will fix the issue. It's as idiotic as it may sound.
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")
}
}
})
}
})
这篇关于“错误域= NEVPNErrorDomain代码= 1 \"(空)\“";“错误域= NEVPNErrorDomain代码= 1".连接VPN服务器时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!