Apps need permission to access sensitive features like camera or location. Permissions handling helps ask users for access safely and respectfully.
Permissions handling in React Native
import { PermissionsAndroid, Platform } from 'react-native'; async function requestCameraPermission() { if (Platform.OS === 'android') { try { const granted = await PermissionsAndroid.request( PermissionsAndroid.PERMISSIONS.CAMERA, { title: 'Camera Permission', message: 'This app needs access to your camera', buttonNeutral: 'Ask Me Later', buttonNegative: 'Cancel', buttonPositive: 'OK', } ); return granted === PermissionsAndroid.RESULTS.GRANTED; } catch (err) { console.warn(err); return false; } } else { // iOS permissions handled differently return true; } }
On Android, you must request permissions at runtime for sensitive features.
On iOS, permissions are usually handled by linking native modules or using libraries.
const granted = await PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION);const hasCameraPermission = await requestCameraPermission(); if (hasCameraPermission) { // proceed to open camera }
react-native-permissions library to check iOS camera permission status.import { check, PERMISSIONS, request, RESULTS } from 'react-native-permissions'; const status = await check(PERMISSIONS.IOS.CAMERA);
This simple app shows a button to request camera permission on Android. It updates the text to show if permission was granted or denied.
import React, { useState } from 'react'; import { View, Text, Button, PermissionsAndroid, Platform } from 'react-native'; export default function App() { const [permissionStatus, setPermissionStatus] = useState('Unknown'); async function requestCameraPermission() { if (Platform.OS === 'android') { try { const granted = await PermissionsAndroid.request( PermissionsAndroid.PERMISSIONS.CAMERA, { title: 'Camera Permission', message: 'This app needs access to your camera', buttonNeutral: 'Ask Me Later', buttonNegative: 'Cancel', buttonPositive: 'OK', } ); if (granted === PermissionsAndroid.RESULTS.GRANTED) { setPermissionStatus('Camera Permission Granted'); } else { setPermissionStatus('Camera Permission Denied'); } } catch (err) { setPermissionStatus('Permission Error'); } } else { setPermissionStatus('iOS Permission Handling Not Shown'); } } return ( <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}> <Text style={{ marginBottom: 20 }}>{permissionStatus}</Text> <Button title="Request Camera Permission" onPress={requestCameraPermission} /> </View> ); }
Always explain to users why you need permissions before asking.
Test permissions on real devices because emulators may behave differently.
Remember to add permission declarations in AndroidManifest.xml or Info.plist files.
Permissions let apps access sensitive device features safely.
Android requires runtime permission requests; iOS uses different methods.
Always check and handle permission results to avoid app crashes.
Practice
Solution
Step 1: Understand app permissions
Permissions are needed to access sensitive features like camera or location.Step 2: Recognize user control
Requesting permissions asks users for approval to protect their privacy.Final Answer:
To get user approval before accessing sensitive device features -> Option AQuick Check:
Permissions = user approval [OK]
- Thinking permissions speed up app loading
- Confusing permissions with UI settings
- Assuming permissions increase storage
Solution
Step 1: Identify correct import source
PermissionsAndroid is part of the core 'react-native' package.Step 2: Check import syntax
Correct syntax uses curly braces for named imports: import { PermissionsAndroid } from 'react-native';Final Answer:
import { PermissionsAndroid } from 'react-native'; -> Option CQuick Check:
Correct import = import { PermissionsAndroid } from 'react-native'; [OK]
- Importing from wrong package
- Missing curly braces for named imports
- Using incorrect module names
async function requestCamera() {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.CAMERA
);
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
console.log('Camera permission granted');
} else {
console.log('Camera permission denied');
}
}
requestCamera();Solution
Step 1: Understand permission request result
If user denies, the result is not 'GRANTED'.Step 2: Check conditional output
Code logs 'Camera permission denied' when permission is denied.Final Answer:
Camera permission denied -> Option AQuick Check:
Denied permission logs denial message [OK]
- Assuming permission is always granted
- Confusing async function behavior
- Ignoring else condition
async function requestLocation() {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.LOCATION
);
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
console.log('Location permission granted');
}
}Solution
Step 1: Check permission constant name
There is no 'LOCATION' permission; correct names are 'ACCESS_FINE_LOCATION' or 'ACCESS_COARSE_LOCATION'.Step 2: Understand impact of wrong permission name
Using wrong permission name causes runtime error or unexpected behavior.Final Answer:
PermissionsAndroid.PERMISSIONS.LOCATION is incorrect permission name -> Option DQuick Check:
Permission name must be exact [OK]
- Using generic or incorrect permission strings
- Forgetting to handle denied permissions
- Misusing async/await syntax
Solution
Step 1: Understand multiple permission requests
Each permission must be requested and checked individually to ensure access.Step 2: Combine results before proceeding
Proceed only if both camera and microphone permissions are granted to avoid errors.Final Answer:
Request camera and microphone permissions separately and check each result before proceeding -> Option BQuick Check:
Check all permissions granted before action [OK]
- Assuming one permission covers both features
- Not awaiting permission results
- Requesting same permission multiple times
Ready to prove it?
Pass the test and this pattern moves unknown
