2

我只有在第一次连接到 VPN 服务器时才收到“Error Domain=NEVPNErrorDomain Code=1 \"(null)\"",后来它工作得很好。

在尝试启动与我的问题类似的 TunnelProvider 网络扩展时检查了 NEVPNErrorDomain Error 1 并按照其在此解决方案中所说的方式进行操作,但仍然出现相同的错误。

所以这是我使用网络扩展连接到 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.
}

注意:我在 GitHub 上找到了相关帖子,但如果我尝试这个解决方案,它会给出 Optional(Error Domain=NEVPNErrorDomain Code=4 "(null)") 问题,因为配置过时,需要加载。您应该调用 loadFromPreferencesWithCompletionHandler: 并在完成处理程序中修改您要修改的值,然后调用 saveToPreferencesWithCompletionHandler:。

提前致谢。

4

2 回答 2

5

解决方案是:

在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")
                }
            }
        })
    }
})
于 2017-11-30T09:29:56.330 回答
0

对我来说,工作了这个解决方案:

  [self save:settings callback:^(NSError *saveError) {
    if (saveError) {
        completionHandler(saveError);
        HMLog(@"Save config failed [%@]", saveError.localizedDescription);
        return;
    }
    [self.vpnManager loadFromPreferencesWithCompletionHandler:^(NSError *e) {  // hack for fix error "NEVPNErrorDomain Code=1"
        [self.vpnManager loadFromPreferencesWithCompletionHandler:^(NSError *loadError) {
            if (loadError) {
                completionHandler(loadError);
                return;
            }
            NSError *a1;
            [self.vpnManager.connection startVPNTunnelAndReturnError:&a1];
            if (a1) {
                completionHandler(a1);
            } else
                completionHandler(nil);
        }];
    }];
}];

保存后只需两次调用 loadFromPreferences。此修复适用于 ios:9、10、11。我不知道为什么,但它的工作......

于 2018-04-12T07:12:48.867 回答