如何在应用程序终止后断开VPN ios swift

发布于 2025-01-17 14:36:49 字数 395 浏览 2 评论 0原文

基本上,我正在使用VPN应用程序,我想在我的应用中对自由用户进行限制。自由用户可以使用1GB的限制连接到VPN,例如TunnelBear应用。

我使用此代码,并且VPN连接/断开连接正常工作。 使用nevpnmanager创建个人VPN

连接> neondemandrule 实现这一目标?

如果我连接到VPN并关闭应用程序VPN在这种情况下仍可以连接到APPN,则在终止应用程序终止时如何将免费用户限制为1GB。

任何建议都会非常有帮助。

Basically i am working on VPN app and i want to put restriction for free user in my app. Free user can connect to VPN with 1GB of limit like TunnelBear app.

I have use this code and VPN connect/disconnect working fine.
Create Personal VPN connection using NEVPNManager

Do i need to add some rule in NEOnDemandRule to achieve this?

If i connect to VPN and close the app VPN still be connected in this case how can i restrict free user limit to 1GB while app terminated.

Any suggestion will be very helpful.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

于我来说 2025-01-24 14:36:50

您的应用将不会在后台持续运行,除非它实现了需要它的功能(例如播放音频、接收位置更新或处理计划任务)。有关详细信息,请参阅 Apple 文档中的这篇有用的文章

处理这种情况的最佳方法是在应用程序发送到后台时暂停计时器,然后在重新进入前台后重新启动计时器。

Your app will not continuously run in the background unless it implements a feature that requires it (e.g. playing audio, receiving location updates, or processing scheduled tasks). See this helpful article in the Apple Docs for more info.

The best way to handle your case is to suspend the timer when the app is sent to the background, and then restart it once it re-enters the foreground.

明天过后 2025-01-24 14:36:50

我使用了这些简单的代码,它的工作原理很完美

import NetworkExtension
            
class yourViewController: UIViewController {
                  
    let manager: NEVPNManager = { NEVPNManager.shared() }()
                
    override func viewDidLoad() {
        super.viewDidLoad()

        //just add this Notification to observe if app terminated
        NotificationCenter.default.addObserver(forName: UIApplication.willTerminateNotification, object: nil, queue: nil) { _ in
            self.manager.connection.stopVPNTunnel()
            print("terminated")
        }
    }
}

I used these simple codes and it works perfect

import NetworkExtension
            
class yourViewController: UIViewController {
                  
    let manager: NEVPNManager = { NEVPNManager.shared() }()
                
    override func viewDidLoad() {
        super.viewDidLoad()

        //just add this Notification to observe if app terminated
        NotificationCenter.default.addObserver(forName: UIApplication.willTerminateNotification, object: nil, queue: nil) { _ in
            self.manager.connection.stopVPNTunnel()
            print("terminated")
        }
    }
}
莫多说 2025-01-24 14:36:50

您需要自己实施 VPN,即通过数据包隧道提供商实施。
然后您将能够计算 1GB 的流量并断开 VPN。

You'll need to implement the VPN yourself, i.e. with a Packet Tunnel Provider.
Then you'll be able to count 1GB of traffic and to disconnect the VPN.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文