我仅在连接到VPN服务器时才第一次收到“错误域= NEVPNErrorDomain代码= 1 \“(空)\””,之后再完美地运行。

我检查了类似于我的问题的NEVPNErrorDomain Error 1 when trying to start TunnelProvider network extension,并按照其在此解决方案中所说的方法进行操作,但仍然遇到相同的错误。

因此,这是我使用网络扩展连接到VPN Server的代码。

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")
                }
            }
        })
    }
})

08-26 03:29