5

My app fetches user's current location in the background and when app is in killed state.

So it needs "Always allow" permission level for this, but in iOS I am getting only two options, "Allow once" and "While in use".

I am using permission_handler package for requesting permission;

 PermissionStatus permission = await Permission.locationAlways.request();

but still, it's not showing "always allow" option. enter image description here

This issue only in iOS. In Android, everything works ok.

1

1 Answer 1

7

As the documentation of permission_handler says:

The locationAlways permission can not be requested directly, the user has to request the locationWhenInUse permission first. Accepting this permission by clicking on the 'Allow While Using App' gives the user the possibility to request the locationAlways permission. This will then bring up another permission popup asking you to Keep Only While Using or to Change To Always Allow.

And that's the reason why is not appearing in the first dialog.

Now, in order to show the second dialog where you can ask user for always allow, you have to ask for permission When in use, and then, validating the status, ask for the second permission

You can do something like this:

    var status = await Permission.locationWhenInUse.status;
if(!status.isGranted){
  var status = await Permission.locationWhenInUse.request();
  if(status.isGranted){
    var status = await Permission.locationAlways.request();
    if(status.isGranted){
      //Do some stuff
    }else{
      //Do another stuff
    }
  }else{
    //The user deny the permission
  }
  if(status.isPermanentlyDenied){
    //When the user previously rejected the permission and select never ask again
    //Open the screen of settings
    bool res = await openAppSettings();
  }
}else{
  //In use is available, check the always in use
  var status = await Permission.locationAlways.status;
  if(!status.isGranted){
    var status = await Permission.locationAlways.request();
    if(status.isGranted){
      //Do some stuff
    }else{
      //Do another stuff
    }
  }else{
    //previously available, do some stuff or nothing
  }
}

Happy coding!

1
  • 1
    This deserves a lot of upvotes! Thanks friend, really helped me Feb 26 at 14:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.