CodeBlanks Logo Icon CodeBlanks

Location permission

Learn to ask for location permission.

Location permission

Location permission is the approval required for the app to receive GPS data. It is requested with Permissions.askAsync(Permissions.LOCATION). If status === 'granted' the location can be fetched; otherwise the map or nearby places feature will not work.

Location permission has two types: foreground (while the app is open) and background (when closed). In Expo Permissions.LOCATION is for foreground; background needs extra setup. Background location is more sensitive; users are more cautious about it. That is why it should be requested only when truly needed. Location is personal data; asking with a good explanation keeps user trust.

Foreground vs background

Foreground location reads GPS data while the app is open on screen. Permissions.LOCATION gives this. It is enough for showing a map, listing nearby places.

Background location reads location even when the app is closed. This is needed for apps that require continuous tracking like navigation or fitness. It is more sensitive; App Store and Play Store review it strictly. Background location requires UIBackgroundModes on iOS and the ACCESS_BACKGROUND_LOCATION permission on Android. Expo configures these in app.json. Asking for background location without need can be intrusive enough to drive users away.

Practice Write requestLocationPermission

Write a requestLocationPermission function. Make it async. Call Permissions.askAsync(Permissions.LOCATION) with await; return status.

Starter code

App.js
// Write the requestLocationPermission function
// Ask LOCATION permission with Permissions.askAsync; give back the status

Sample solution

App.js
async function requestLocationPermission() {
  const { status } = await Permissions.askAsync(Permissions.LOCATION);
  return status;
}

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Match each type to its role

Term Meaning
Permissions.LOCATION Foreground location
background Location while closed
GPS Location source
'granted' Permission given

2 LOCATION permission

Question
await Permissions.askAsync(Permissions.____);
Answer
await Permissions.askAsync(Permissions.LOCATION);

Explanation: Permissions.LOCATION is the location permission.

3 Why is location permission requested?

App.js
Permissions.askAsync(Permissions.LOCATION);
  • To get GPS data Correct answer
  • To press a button
  • To send notifications
  • To store files

Explanation: Location permission is requested to get GPS data.

All lessons in Intermediate