React Native
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.
Exercise Write requestCameraPermission
Write a requestCameraPermission function. Make it async. Call Permissions.askAsync(Permissions.CAMERA) with await; take status from the result and return it.
Show example solution
async function requestCameraPermission() {
const { status } = await Permissions.askAsync(Permissions.CAMERA);
return status;
}
Test yourself
-
Match each method to its role
- askAsync
- Asks permission
- getAsync
- Reads current state
- status
- Result field
- Permissions.CAMERA
- Camera permission
-
Permissions import
import * as ____ from 'expo-permissions';
Show answer
import * as Permissions from 'expo-permissions';
-
What does askAsync return?
const { status } = await Permissions.askAsync(Permissions.CAMERA);
- A string
- A number
- An object
- An array
Show answer
An object — askAsync returns an object with a status field.
Next lesson: Camera permission