Rahul Bhardwaj
Rahul Bhardwaj

Reputation: 13

Unhandled Exception: PlatformException(PERMISSION_DENIED, Access to location data denied, null)

   it give the exception when i get the current  location of the user . my flutter version :-  
    v1.17.4, and  my info.plist code is given below. geolocator: ^5.3.2+2


Future<Position> locateUser() async {
  return  await Geolocator()
      .getCurrentPosition(desiredAccuracy: LocationAccuracy.low,locationPermissionLevel: 
         GeolocationPermission.location);
              }
<key>NSLocationWhenInUseUsageDescription</key>
<string>This app needs access to location when open.</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>This app needs access to location when in the background.</string>
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>This app needs access to location when open and in the background.</string>


 

Upvotes: 0

Views: 3975

Answers (2)

Amit Baderia
Amit Baderia

Reputation: 4882

I was facing the same issue. I was running on IOS simulator

Issue Resolved on calling requestPermission() method

LocationPermission permission = await Geolocator.requestPermission();

Future<Position> position =
    Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);

In info.plist:

<key>NSLocationWhenInUseUsageDescription</key>
<string>This app needs access to your location.</string>

dependency in pubspec.yaml:

geolocator: ^8.0.5

Upvotes: 1

Nitesh Malviya
Nitesh Malviya

Reputation: 840

It might be late to reply but I was also facing the same problem where the app was not asking for permission in iOS and was working perfectly fine in android.

Because it was not asked for permission that's why the permission code was not working for iOS. I found a package named "location_permissions" which can be used to ask for permission manually.

Steps to do are following

  1. Add "location_permissions: 3.0.0+1" this dependencies in "pubspec.yaml". Please note that I did that for flutter 1.22.0 so for flutter 2.0 this might be an issue.

  2. Import the package in the file

    import 'package:location_permissions/location_permissions.dart';
    
  3. Add the following code on the page where you want to ask for permission. (Better to add that on the very first page of your app.)

      @override
       void initState() {
        ....
       if (Platform.isIOS) {
         location_permission();
       }
       ....
    }
    
  4. Add the following two methods in the same file

    void location_permission() async {
     final PermissionStatus permission = await _getLocationPermission();
     if (permission == PermissionStatus.granted) {
       final position = await geolocator.getCurrentPosition(
           desiredAccuracy: LocationAccuracy.best);
    
       // Use the position to do whatever...
     }
    }
    
    Future<PermissionStatus> _getLocationPermission() async {
     final PermissionStatus permission = await LocationPermissions()
         .checkPermissionStatus(level: LocationPermissionLevel.location);
    
     if (permission != PermissionStatus.granted) {
       final PermissionStatus permissionStatus = await LocationPermissions()
           .requestPermissions(
               permissionLevel: LocationPermissionLevel.location);
    
       return permissionStatus;
     } else {
       return permission;
     }
    }
    

That's it now you should get a popup in the iOS app which will ask for the permission of location.

Upvotes: 2

Related Questions