I have scenario when user opted for 'never show again'. If so, i can open setting for them with openAppSettings() .
Is it possible to wait until user gets back to application?
I want check permission again after user came back and, if granted, do some action.
Example code:
final opened = await openAppSettings(); //opens App'wide system settings
if (!opened) {
//do some error handling
}
final granted = await Permission.location.isGranted;
if (granted) {
afterPermissionGranted(); //do some action after permission granted
}
It could be nice have some callBack which will be notified when user came back.
The problem is a bit that the native SDK's don't allow us to register a callback method that is called when a user returns.
As far as I understand (and how I solved it several times when doing native iOS development), the idea is that you listen for the App getting focus again. When you call the openAppSettings the platform will redirect the user to the settings screen and your App loses focus. When the user finishes up in the settings and moves back to your App, it will get focus again. This would trigger a callback and that would be an ideal place to check for permissions again. With Flutter you can possible make use of the AppLifecycleState (see here for more detaisl) to get notified when the user comes back to the App. Note that this is not something I have tried yet, so I am not sure this will do the trick. Since this is something that is related to the state of the App, it is difficult and not really the right place to implement this in the permission_handler plugin.
If you have any other ideas or suggestions please let me know.
The problem is a bit that the native SDK's don't allow us to register a callback method that is called when a user returns.
* On iOS we use the open method which allows registering a callback which will inform us if the URL was opened successfully or not. Unfortunately this doesn't tell us anything about it the user came back to the app or make any changes (see the iOS docs for more info); * On Android we send the ACTION_APPLICATION_DETAILS_SETTINGS intent. Also here there is no way to register any callback about returning or being informed about changes to the settings.As far as I understand (and how I solved it several times when doing native iOS development), the idea is that you listen for the App getting focus again. When you call the openAppSettings the platform will redirect the user to the settings screen and your App loses focus. When the user finishes up in the settings and moves back to your App, it will get focus again. This would trigger a callback and that would be an ideal place to check for permissions again. With Flutter you can possible make use of the AppLifecycleState (see here for more detaisl) to get notified when the user comes back to the App. Note that this is not something I have tried yet, so I am not sure this will do the trick. Since this is something that is related to the state of the App, it is difficult and not really the right place to implement this in the permission_handler plugin.
If you have any other ideas or suggestions please let me know.
Yes. Exactly. Thanks for the suggestion. Before you wrote, I had already solution.
I've just used WidgetsBindingObserver, basically
class AppState extends State<App>
with WidgetsBindingObserver {
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
}
// THIS is called whenever life cycle changed
void didChangeAppLifecycleState(AppLifecycleState state) async {
if (state == AppLifecycleState.resumed) {
final granted = await Permission.location.isGranted;
if (granted) {
//do whatever you want
}
}
}
@override
Widget build(BuildContext context) {
//...
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
Thank you for the feedback, this might also help others.
Most helpful comment
Yes. Exactly. Thanks for the suggestion. Before you wrote, I had already solution.
I've just used WidgetsBindingObserver, basically