3

I am trying to setup a IPSec VPN connection but when I try to run

manager.saveToPreferences

or

manager.loadFromPreferences

The callback functions are never called and so I cannot start the VPN Tunnel, and "123" is never printed. The program just ends. What am I doing wrong?

I am testing this in MacOS 10.13.4

import NetworkExtension
import Foundation


let manager = NEVPNManager.shared()
let p = NEVPNProtocolIPSec()
p.authenticationMethod = NEVPNIKEAuthenticationMethod.sharedSecret
p.remoteIdentifier = remoteID
p.localIdentifier = localID
KeychainWrapper.standard.set("SECRET", forKey: "SECRET")
p.sharedSecretReference = KeychainWrapper.standard.dataRef(forKey: "SECRET");

manager.protocolConfiguration = p
manager.onDemandRules = [NEOnDemandRuleConnect()]
manager.isOnDemandEnabled = true
manager.isEnabled = true

manager.saveToPreferences { completionHandler in
    manager.loadFromPreferences { completionHandler in
        print(123)
        do {
            try manager.connection.startVPNTunnel()
        } catch (let exception) {
            print(exception)
        }
    }
}

I am also having issues configuring the VPN, but this question is more about why those callbacks never get called.

Also, as a side note. When I run

    do {
        try manager.connection.startVPNTunnel()
    } catch (let exception) {
        print(exception)
    }

outside of saveToPreferences and loadFromPreferences I get the following error:

Error Domain=NEVPNErrorDomain Code=1 "(null)"

Any help would be greatly appreciate, thanks.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Max00355
  • 827
  • 2
  • 12
  • 28

1 Answers1

0

Is this just in a .swift file by itself? Try putting the code in a simple application, like inside applicationDidFinishLaunching(). It probably won’t do anything without a running runloop

Brendan Shanks
  • 3,141
  • 14
  • 13