gpt4 book ai didi

ios - "Error Domain=NEVPNErrorDomain Code=1\"(null)\""连接 VPN 服务器时

转载 作者:可可西里 更新时间:2023-11-01 05:32:39 62 4
gpt4 key购买 nike

我在第一次连接到 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/

62 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com