【问题标题】:stopVPNTunnel is not disconnecting the VPN connection in iOS停止 VPN 隧道不会断开 iOS 中的 VPN 连接
【发布时间】:2022-01-03 14:14:27
【问题描述】:

我已经实现了一个成功的 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()
}

我不知道为什么它没有断开连接。

【问题讨论】:

  • 一团糟,但是在创建新的NETunnelProviderManager 并使用saveToPreferences() 将其保存到首选项后,您总是需要调用tunnelProvider.loadFromPreferences()。所以也许这就是原因。
  • 您能否向我推荐任何符合您想法的资源?这将非常有帮助。我已经被困在这里3天了。

标签: ios swift vpn nevpnmanager


【解决方案1】:

loadFromPreferences闭包内调用stopVPNTunnel()

NEVPNManager.shared().loadFromPreferences { error in
  assert(error == nil, "Failed to load preferences: \(error!.localizedDescription)")
  NEVPNManager.shared().connection.stopVPNTunnel()
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-30
    • 2023-01-24
    • 1970-01-01
    • 2016-09-29
    • 1970-01-01
    • 1970-01-01
    • 2011-12-16
    • 2020-04-14
    相关资源
    最近更新 更多