CodeBlanks Logo Icon CodeBlanks

Camera permission

Learn to ask for camera permission.

Camera permission

Camera permission is the approval required for the app to open the camera. It is requested with Permissions.askAsync(Permissions.CAMERA). If status === 'granted' the camera can open; otherwise the user should be guided.

The camera is one of the most sensitive permissions; users approach it cautiously. That is why it is more natural to ask when the user taps 'Take photo', not at launch. If denied, the camera cannot open; a short message can explain why it is needed. On iOS you must add NSCameraUsageDescription to Info.plist; otherwise the app crashes. Expo takes this description from ios.infoPlist in app.json.

A two-step flow

A two-step flow is common for camera permission. First you check the current state with Permissions.getAsync(Permissions.CAMERA). If 'granted' the camera opens. If 'denied' the user is routed to settings. If 'undetermined' you ask for the first time with askAsync.

This flow avoids showing the user an unnecessary dialog. getAsync does not show a dialog; it only reads the saved state. The 'undetermined' state means it has never been asked before; then askAsync is called. Asking again with askAsync for a denied permission does not show the dialog; that is why routing to settings is needed. This flow is the backbone of a realistic camera feature.

Practice Write the useCameraPermission hook

Write a useCameraPermission hook. Hold status with useState('undetermined'). Inside useEffect call Permissions.askAsync(Permissions.CAMERA); in .then call setStatus. Return status.

Starter code

App.js
// Write the useCameraPermission hook
// Hold status with useState; ask permission in useEffect; return status

Sample solution

App.js
import { useState, useEffect } from "react";
import * as Permissions from "expo-permissions";

export function useCameraPermission() {
  const [status, setStatus] = useState("undetermined");
  useEffect(() => {
    Permissions.askAsync(Permissions.CAMERA).then(({ status }) => {
      setStatus(status);
    });
  }, []);
  return status;
}

Questions and answers

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

1 status granted check

Question
if (status === ____) { /* kamera aç */ }
Answer
if (status === 'granted') { /* kamera aç */ }

Explanation: status 'granted' means the camera can open.

2 What happens if camera permission is denied?

App.js
if (status !== 'granted') { /* ... */ }
  • The camera opens anyway
  • The app crashes
  • The device restarts
  • The camera cannot open Correct answer

Explanation: If camera permission is denied, the camera cannot open.

3 Match each flow step to its role

Term Meaning
getAsync Reads current state
askAsync Asks first time
'granted' Permission given
'undetermined' Never asked

All lessons in Intermediate