CodeBlanks Logo Icon CodeBlanks

expo-permissions

Learn to ask for permissions with expo-permissions.

What is expo-permissions?

expo-permissions is Expo's module for asking permissions. Permissions.askAsync(Permissions.CAMERA) requests a permission; the result is an object with a status field. status has three values: 'granted', 'denied', 'undetermined'.

Usage: import with import * as Permissions from 'expo-permissions', then request with await Permissions.askAsync(Permissions.CAMERA). askAsync returns a promise; that is why it is called with async/await. getAsync reads the current state without asking. The first request shows a system dialog; later calls do not show it again. In Expo projects this module is included by default.

askAsync

Permissions.askAsync(permissionType) requests a permission. On the first call it shows a system dialog; later calls return the remembered result. It returns a promise; you wait with await.

The returned object is shaped like { status, expires, permissions }. The most used field is status. Example: const { status } = await Permissions.askAsync(Permissions.CAMERA). If status === 'granted' the camera can open; otherwise the user should be guided. The expires field tells when the permission expires on iOS; on Android it is usually 'never'. This detail can be ignored in most cases.

Practice Write requestCameraPermission

Write a requestCameraPermission function. Make it async. Call Permissions.askAsync(Permissions.CAMERA) with await; take status from the result and return it.

Starter code

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

Sample solution

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

Questions and answers

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

1 Match each method to its role

Term Meaning
askAsync Asks permission
getAsync Reads current state
status Result field
Permissions.CAMERA Camera permission

2 Permissions import

Question
import * as ____ from 'expo-permissions';
Answer
import * as Permissions from 'expo-permissions';

Explanation: Permissions is imported from expo-permissions.

3 What does askAsync return?

App.js
const { status } = await Permissions.askAsync(Permissions.CAMERA);
  • A string
  • A number
  • An object Correct answer
  • An array

Explanation: askAsync returns an object with a status field.

All lessons in Intermediate