Disconnect VPN when user Kill the app
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?
Having the same issue. Tried engine.disconnect() in dispose() but it doesn't seem to work. Although it works in reassemble().
@SpaceXM did you find a workaround?
Hi @SpaceXM I just noticed that subscribing to didChangeAppLifecycleState and checking if AppLifecycleState.detached then disconnecting works on Android but not on iOS.
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.
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
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;
}
}
@ahmdsdk @SpaceXM did you find anything?, I need urgent help
This is what i did with the use of provider as well
//globalUtils is a class i created that has my dialog structure
Future
return Future.value(true);
}
}
@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.
@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
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() } } } }