“Error Domain=NEVPNErrorDomain Code=1 \\”(null)\\“” While connecting VPN server

让人想犯罪 __ 提交于 2019-11-30 22:42:58
Mukesh

Solution is :

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

For me, worked this solution:

  [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);
        }];
    }];
}];

Just twice call loadFromPreferences, after saving. This fix work on ios: 9, 10, 11. I don't know why, but its work....

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!