如何在 applicationWillTerminate 方法中调用 Web 服务并停止 VPN 服务器?

How to call web service and stop VPN server in applicationWillTerminate Method?

我已经实现了 VPN 服务器逻辑 (NETunnelProviderManager) 并使用 startTunnel(options: ["ipv4": dnsIpv4]) 方法在 NETunnelProviderSession 上启动隧道。 VPN 工作正常并正常启动。我想在应用程序终止时在 applicationWillTerminate(_ application: UIApplication) 方法中停止 VPN 隧道。我也为此添加了代码逻辑,但它不会停止 VPN 而且我还想在应用程序终止时调用 API(Web 服务)。

func applicationWillTerminate(_ application: UIApplication) {

//停止VPN self.vpnService.disable { (错误)在 }

//执行API调用 self.myAPI()

}

Your implementation of this method has approximately five seconds to perform any tasks and return. If the method does not return before time expires, the system may kill the process altogether.

来源:https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1623111-applicationwillterminate