Flutter permission_handler : request location permission on iOS

Viewed 1211

I want to ask user to enable permission only if he denied the permission or the permission is not allowed This function is working very well on android

  Future _getLocationPermission() async {
    if (await Permission.location.request().isGranted) {
      permissionGranted = true;
    } else if (await Permission.location.request().isPermanentlyDenied) {
      throw('location.request().isPermanentlyDenied');
    } else if (await Permission.location.request().isDenied) {
      throw('location.request().isDenied');
      permissionGranted = false;
    }
  }

but on iOS it throw exception permission isPermanentlyDenied Unhandled Exception: location.request().isPermanentlyDenied even if the user allowed location permission while using app I'm using permission_handler package

3 Answers

iOS is stricter, it does not allow you to request for permission if the user has already permanently denied it. In this case your only option is to inform the user about this, and offer the possibility to open application settings and grant permission there. This will likely restart your application if the user grants.

So check the status without requesting:

final status = await Permission.location.status;

If status is permanently denied, display a Flutter dialog (you can't use the system permission grant dialog in this case):

if (status == PermissionStatus.permanentlyDenied) {
  // display a dialog, explain the user that he/she can grant 
  // permission only in the phone's application settings
}

If the user want's to do it, you can route to the application settings:

openAppSettings(); // this is a method of the permission handler package

This method is a future, but in my experience you don't need to await it.

Please check this link. You must have to mention permission in POD file also.

This is just a function that returns bool if the user enables location or not simple

Future<bool> canGetLocation(Location location) async {
      if (!await location.requestService()) return false;
       final status = await location.requestPermission();
       final granted = PermissionStatus.granted;
       final grantedLimited = PermissionStatus.grantedLimited;
       bool result = status == granted || status == granted;
      return result;
    } 
    
    if (await canGetLocation(location)) {
         // Do something
        } else {
          final status = await handler.Permission.location.status;
          if (status.isPermanentlyDenied || status.isDenied) {
           // ask the user to open the app setting 
           // from permission handler package you have 
               openAppSettings(); 
          } else {
            // show info dialog or something that the user need to enable location services
          }
        }
Related