-1

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.

Kuldeep
  • 4,466
  • 8
  • 32
  • 59
  • You don't have direct control as to how much time your app gets in the background or when your app will run in the background. With that being said, you have some things you can do. Have a look at https://developer.apple.com/documentation/uikit/app_and_environment/scenes/preparing_your_ui_to_run_in_the_background/extending_your_app_s_background_execution_time and https://www.raywenderlich.com/5817-background-modes-tutorial-getting-started – Shawn Frank Mar 28 '22 at 07:11
  • Hi @ShawnFrank, Thanks for you comment. Updated question, can you please check it. – Kuldeep Mar 28 '22 at 12:28

3 Answers3

0

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.

Eric33187
  • 1,006
  • 7
  • 13
  • Thanks for your reply. Updated question with requirement. Can you please check it – Kuldeep Mar 28 '22 at 12:27
  • 1
    I'm not familiar enough with VPN apps to give you an answer on how to limit VPN service to 1GB; however, I suspect the solution will **not** be to set a timer to constantly check usage. The solution would have to be somewhere related to the code for implementing VPNs, or perhaps within the external VPN service itself. – Eric33187 Mar 29 '22 at 06:50
  • 1
    Also, this seems to be a completely different question now, so you would probably get better help by posting a new question, and returning the question (with the previous comments and answers people worked on) in their original form. – Eric33187 Mar 29 '22 at 06:50
0

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")
        }
    }
}
Amro Jaber
  • 185
  • 1
  • 17
-1

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.

Witterquick
  • 6,048
  • 3
  • 26
  • 50