Home > OS >  "Location Permission Denied" on ios simulator even after setting custom location in Featur
"Location Permission Denied" on ios simulator even after setting custom location in Featur

Time:03-16

I am new to mobile development. I am trying to access my current location in the ios simulator running IOS 14.4 and I keep getting the "Location Permission denied" error even after setting up the custom location in the simulator as suggested by some answers on this platform, here. The code that I am running is this

Geolocation.getCurrentPosition(
    position => {
      this.setState({
        latitude: position.coords.latitude,
        longitude: position.coords.longitude,
        coordinates: this.state.coordinates.concat({
          latitude: position.coords.latitude,
          longitude: position.coords.longitude
        })
      });
    },
    error => {
      Alert.alert(error.message.toString());
    },
    {
      showLocationDialog: true,
      enableHighAccuracy: true,
      timeout: 20000,
      maximumAge: 0
    }
  );

I have set up these permissions in my info-plist.

<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>Description</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>Will you allow this app to always know your location?</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>Do you allow this app to know your current location?</string>

I am not entirely sure what I might be doing wrong here. This is the only code currently that tries to access the location.

Is there any other reason why this error should persist even after setting the current location as the custom location in Features->Location->customlocation? as suggested by the other answers on this platform.

CodePudding user response:

It's required to ask location access permission at run-time before you run code that requires location access.

react-native-permissions is the standard library for cross-platform permissions

Follow installation instruction here - https://www.npmjs.com/package/react-native-permissions

import { request, PERMISSIONS, RESULT } from "react-native-permissions";

function getUserLocation() {
  Geolocation.getCurrentPosition(
    (position) => {
      this.setState({
        latitude: position.coords.latitude,
        longitude: position.coords.longitude,
        coordinates: this.state.coordinates.concat({
          latitude: position.coords.latitude,
          longitude: position.coords.longitude,
        }),
      });
    },
    (error) => {
      Alert.alert(error.message.toString());
    },
    {
      showLocationDialog: true,
      enableHighAccuracy: true,
      timeout: 20000,
      maximumAge: 0,
    }
  );
}
request(PERMISSIONS.IOS.LOCATION_ALWAYS).then((result) => {
  switch (result) {
    case RESULTS.UNAVAILABLE:
      console.log(
        "This feature is not available (on this device / in this context)"
      );
      break;
    case RESULTS.DENIED:
      console.log(
        "The permission has not been requested / is denied but requestable"
      );
      break;
    case RESULTS.LIMITED:
      console.log("The permission is limited: some actions are possible");
      break;
    case RESULTS.GRANTED:
      console.log("The permission is granted");
      // Permission has been granted - app can request location coordinates
      getUserLocation();
      break;
    case RESULTS.BLOCKED:
      console.log("The permission is denied and not requestable anymore");
      break;
  }
});



  • Related