我已经实现了一个成功的vpn连接。但是,当我在连接VPN时关闭并打开应用程序时,我无法断开VPN。
public func connectVPN() {
//For no known reason the process of saving/loading the VPN configurations fails.On the 2nd time it works
Log.d(message: "connectVPN")
NSLog("ConnectVPN")
self.vpnManager.loadFromPreferences(completionHandler: self.vpnLoadHandler)
NotificationCenter.default.addObserver(forName: NSNotification.Name.NEVPNStatusDidChange, object: nil , queue: nil) {
notification in
let nevpnconn = notification.object as! NEVPNConnection
let status = nevpnconn.status
self.vpnDelegate?.checkNES(status: status)
}
}
public func disconnectVPN() ->Void {
Log.d(message: "Disconnect VPN Called")
Log.d(message: "Log Disconnect VPN Called")
print("vpnManager:disconnectVPN (self.vpnManager.connection) ") // object available
self.vpnManager.connection.stopVPNTunnel()
}
我不知道为什么它没有断开。
在loadFromPreferences
闭包内调用stopVPNTunnel()
。
NEVPNManager.shared().loadFromPreferences { error in
assert(error == nil, "Failed to load preferences: (error!.localizedDescription)")
NEVPNManager.shared().connection.stopVPNTunnel()
}