openvpn_flutter icon indicating copy to clipboard operation
openvpn_flutter copied to clipboard

Disconnect VPN when user Kill the app

Open SpaceXM opened this issue 3 years ago • 11 comments

There is any way to perform a VPN disconnection when user kill the app outside my exit button? Any workaround to avoid VPN stay active also when app is closed?

SpaceXM avatar Nov 28 '22 12:11 SpaceXM

Having the same issue. Tried engine.disconnect() in dispose() but it doesn't seem to work. Although it works in reassemble().

ahmdsdk avatar Dec 02 '22 15:12 ahmdsdk

@SpaceXM did you find a workaround?

ahmdsdk avatar Dec 03 '22 12:12 ahmdsdk

Hi @SpaceXM I just noticed that subscribing to didChangeAppLifecycleState and checking if AppLifecycleState.detached then disconnecting works on Android but not on iOS.

ahmdsdk avatar Dec 03 '22 23:12 ahmdsdk

I don't think this question is in scope of this plugin. You should detect app's closing and call disconnect method by yourself. For iOS it's trickier and you should probably use some native capabilities provided by iOS.

0ttik avatar Dec 05 '22 12:12 0ttik

Hi @SpaceXM I just noticed that subscribing to didChangeAppLifecycleState and checking if AppLifecycleState.detached then disconnecting works on Android but not on iOS.

i will try this way for android, still searching for IOS

dispose not work just because is called ONLY when user do .pop or close the app using your coded exit button

SpaceXM avatar Dec 10 '22 13:12 SpaceXM

Hi @SpaceXM I just noticed that subscribing to didChangeAppLifecycleState and checking if AppLifecycleState.detached then disconnecting works on Android but not on iOS.

i will try this way for android, still searching for IOS

dispose not work just because is called ONLY when user do .pop or close the app using your coded exit button

if you use provider to configure the vpn you will be able to disconnect the app on whatever screen

void disconnectVPN(context) async {
    var openVPN = Provider.of<OpenVPNModel>(context, listen: false);
    if (openVPN.connected) {
      print("disconnect");
      openVPN.engine!.disconnect();

      openVPN.connected = false;
    }
  }

Oshaine avatar Feb 14 '23 17:02 Oshaine

@ahmdsdk @SpaceXM did you find anything?, I need urgent help

syedabdulbasit1 avatar Nov 08 '23 10:11 syedabdulbasit1

This is what i did with the use of provider as well

//globalUtils is a class i created that has my dialog structure

Future _onWillPop() async { if (_selectedIndex == 1) { final dialogResult = await GlobalUtils().openDialog( context, 'Are you sure?', 'Do you want to exit the App?', 'No', 'Yes', () { if (Provider.of<OpenVPNModel>(context, listen: false).connected) { OpenVPNConfig().disconnectVPN(context); } Navigator.of(context).pop(true); exit(0); }); // add null check for dialogResult if (dialogResult != null) { return dialogResult; } else { return Future.value(false); } } else { final navigation = Provider.of<NavigationModel>(context); navigation.selectedIndex = 1;

  return Future.value(true);
}

}

Oshaine avatar Nov 08 '23 14:11 Oshaine

@Oshaine Thank you, for sharing the solution, but what happens when the user kills the app manually like from the background? But anyway I just change the app flow when the user kill the app and then restart again then VPN will disconnect.

syedabdulbasit1 avatar Nov 09 '23 12:11 syedabdulbasit1

@Oshaine Thank you, for sharing the solution, but what happens when the user kills the app manually like from the background? But anyway I just change the app flow when the user kill the app and then restart again then VPN will disconnect.

No, is not working in this way when user kills the app because no fuction can be fired on this event

SpaceXM avatar Nov 09 '23 13:11 SpaceXM

Replace AppDelegate.swift under runner in Xcode with the following code, and the program will terminate successfully. import Flutter import UIKit import NetworkExtension @UIApplicationMain @objc class AppDelegate: FlutterAppDelegate{ var providerManager: NETunnelProviderManager! override func application( _ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? ) -> Bool { GeneratedPluginRegistrant.register(with: self) return super.application(application, didFinishLaunchingWithOptions: launchOptions) } override func applicationWillTerminate(_ application: UIApplication) { self.providerManager.connection.stopVPNTunnel() } override func applicationDidBecomeActive(_ application: UIApplication) { super.applicationDidBecomeActive(application) loadProviderManager() } func loadProviderManager() { NETunnelProviderManager.loadAllFromPreferences { (managers, error) in if error == nil { self.providerManager = managers?.first ?? NETunnelProviderManager() } } } }

baijingtong avatar Jun 26 '24 03:06 baijingtong