I’ve been following a guide to build a location tracker, but I constantly get the error
“Error: One of the NSLocation*UsageDescription
keys must be present in Info.plist to be able to use geolocation.”
And
“Error: Background Location has not been configured. To enable it, add location
to UIBackgroundModes
in Info.plist file.”
Which doesn’t make much sense since I already put both of the required stuff in the app.json
import React, { Component, useEffect, useState } from "react";
import { StyleSheet, Text, View } from "react-native";
import MapView from "react-native-maps";
import * as Location from "expo-location";
import * as TaskManager from "expo-task-manager";
const LOCATION_TASK_NAME = "background-location-task";
export default function Screen() {
const [region, setRegion] = useState<{
latitude: number;
longitude: number;
latitudeDelta: number;
longitudeDelta: number;
heading: number | null;
}>({
latitude: 0,
longitude: 0,
latitudeDelta: 0.045,
longitudeDelta: 0.045,
heading: null,
});
const getLocationAsync = async () => {
Location.startLocationUpdatesAsync(LOCATION_TASK_NAME, {
accuracy: Location.Accuracy.BestForNavigation,
distanceInterval: 1,
timeInterval: 500,
});
const newLocation = await Location.watchPositionAsync(
{
accuracy: Location.Accuracy.BestForNavigation,
distanceInterval: 1,
timeInterval: 10000,
},
({ coords }) => {
setRegion((prev) => ({
...prev,
latitude: coords.latitude,
longitude: coords.longitude,
heading: coords.heading,
}));
}
);
return newLocation;
};
const permissionHandler = async () => {
const { status } = await Location.requestForegroundPermissionsAsync();
if (status === "granted") {
// const { status: backgroundStatus } =
// await Location.requestBackgroundPermissionsAsync();
// if (backgroundStatus === "granted") {
getLocationAsync();
// }
}
};
useEffect(() => {
permissionHandler();
}, []);
return <Text>Test</Text>;
}
TaskManager.defineTask(LOCATION_TASK_NAME, async ({ data, error }: any) => {
if (error) {
console.log(error);
return;
}
if (data) {
const { locations } = data;
let lat = locations[0].coords.latitude;
let long = locations[0].coords.longitude;
console.log(lat, long);
}
});
App.json
{expo:{
...
"permissions": [
"location",
"fetch"
],
"ios": {
"supportsTablet": true,
"infoPlist": {
"UIBackgroundModes": [
"location",
"fetch",
"remote-notification"
],
"NSLocationWhenInUseUsageDescription": "Your app needs access to location when in use for...",
"NSLocationAlwaysUsageDescription": "Your app needs access to location even when the app is in the background for...",
"NSLocationAlwaysAndWhenInUseUsageDescription": "Your app needs access to location even when the app is in the background for..."
}
},
"plugins": [
[
"expo-location",
{
"locationAlwaysAndWhenInUsePermission": "Allow $(PRODUCT_NAME) to use your location.",
"locationAlwaysPermission": "Allow $(PRODUCT_NAME) to use your location."
}
]
]
}}
I tried clearing the cache, didn't work, resetting the app, also didn't work....