Error Domain=NEVPNErrorDomain Code=4 “configuration is stale” UserInfo={NSLocalizedDescription=configuration is stale} While connecting to VPN Server

I’m working on a VPN Application, and in my application I’m trying to connect to VPN using IkEv2 protocol. But I’m unable to connect to VPN. Im using UISwitch to connect/disconnect to VPN server. Here is my code:

storing VPN Password in Keychain..

override func viewDidLoad() {

super.viewDidLoad()

let keychain = KeychainSwift()
keychain.set("*****", forKey: "vpnPassword")

NotificationCenter.default.addObserver(self, selector: #selector(ViewController.VPNStatusDidChange(_:)), name: NSNotification.Name.NEVPNStatusDidChange, object: nil)

Switch to connect/disconnect VPN server..

 @IBAction func switchButton(_ sender: UISwitch) {

    switchConntectionStatus.isOn = false

    if !isConnected {
        initVPNTunnelProviderManager()
    }
    else{

        //Removing the current VPN configuration from the caller's VPN preferences..
        vpnManager.removeFromPreferences(completionHandler: { (error) in

            if((error) != nil) {
                print("VPN Remove Preferences error: 1")
            }
            else {
                self.vpnManager.connection.stopVPNTunnel()
                self.labelConntectionStatus.text = "Disconnected"
                self.switchConntectionStatus.isOn = false
                self.isConnected = false
            }
        })
    }
}

initiating VPN tunnel..

func initVPNTunnelProviderManager(){

    //loading the current VPN configuration from the caller's VPN preferences..
    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 = KeychainService()
            let data = keychain.load(key: "vpnPassword")

            p.passwordReference = data
            p.authenticationMethod = NEVPNIKEAuthenticationMethod.none



            p.useExtendedAuthentication = true
            p.disconnectOnSleep = false

            self.vpnManager.protocolConfiguration = p
            self.vpnManager.isEnabled = true

            ////saving the current VPN configuration from the caller's VPN preferences..
            self.vpnManager.saveToPreferences(completionHandler: { (saveError) -> Void in
                if((saveError) != nil) {
                    print("VPN Preferences save error: (saveError!)")
                }
                else {


                    self.vpnManager.loadFromPreferences(completionHandler: { (loadError) in

                        if((loadError) != nil) {

                            print("VPN Preferences load error: (loadError!)")
                        }
                        else {

                            var startError: NSError?

                            do {
                                try self.vpnManager.connection.startVPNTunnel()
                            }
                            catch let error as NSError {
                                startError = error
                                print("Vpn Manager Connection Error: (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: UIAlertController.Style.alert)
                                alertController.addAction(UIAlertAction(title: "Dismiss", style: UIAlertAction.Style.default,handler: nil))

                                self.present(alertController, animated: true, completion: nil)
                                print("Alert Controller: (startError!)")
                            }
                            else {
                                self.VPNStatusDidChange(nil)
                                print("Start VPN")
                            }

                        }

                    })

                }
            })
        }
    }
}

changing the status accordingly by calling VPNStatusDidChange..

@objc func VPNStatusDidChange(_ notification: Notification?) {

    print("VPN Status changed:")
    let status = self.vpnManager.connection.status
    switch status {
    case .connecting:
        print("Connecting...")
        self.labelConntectionStatus.text = "Connecting..."
        self.switchConntectionStatus.isOn = false
        self.isConnected = false

        break
    case .connected:
        print("Connected")
        self.labelConntectionStatus.text = "Connected"
        self.switchConntectionStatus.isOn = true
        self.isConnected = true
        break
    case .disconnecting:
        print("Disconnecting...")
        self.labelConntectionStatus.text = "Disconnecting..."
        self.switchConntectionStatus.isOn = false
        self.isConnected = false

        break
    case .disconnected:
        print("Disconnected")
        self.labelConntectionStatus.text = "Disconnected..."
        self.switchConntectionStatus.isOn = false
        self.isConnected = false

        break
    case .invalid:
        print("Invalid")
        self.labelConntectionStatus.text = "Invalid Connection"
        self.switchConntectionStatus.isOn = false
        self.isConnected = false

        break
    case .reasserting:
        print("Reasserting...")
        self.labelConntectionStatus.text = "Reasserting Connection"
        self.switchConntectionStatus.isOn = false
        self.isConnected = false

        break
    }
}

Im following this tutorial to save and load password from Keychain.
http://blog.moatazthenervous.com/create-a-key-chain-for-apples-vpn/.

This is how my output looks like..

Archive from: https://stackoverflow.com/questions/59029058/error-domain-nevpnerrordomain-code-4-configuration-is-stale-userinfo-nslocali

Share

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *