React Native Permissions
react-native-permissions provides a unified, cross-platform API for managing user permissions on iOS, Android, and Windows within React Native applications. It abstracts away the platform-specific nuances of checking and requesting permissions like camera, location, and notifications, offering a consistent interface. The current stable version is 5.5.1. The library follows the React Native release support policy, supporting the latest version and the two previous minor series, indicating a regular update cadence aligned with React Native itself. A key differentiator is its modular iOS setup, where developers explicitly enable only the permissions they need via a `Podfile` script, reducing the app's binary size and simplifying configuration, alongside robust TypeScript support and handling of nuances like Android's one-time permissions. It supports Windows builds 18362 and later, extending its reach beyond mobile.
Common errors
-
This app has crashed because it attempted to access privacy-sensitive data without a usage description. The app's Info.plist must contain an NS[PermissionType]UsageDescription key with a string value explaining to the user how the app uses this data.
cause Missing `Info.plist` entry for a requested iOS permission.fixAdd the corresponding `NS[PermissionType]UsageDescription` key (e.g., `NSCameraUsageDescription`) and a descriptive string value to your `Info.plist` file in your iOS project. -
`_NativePermissions.check` is null or not an object.
cause Native module not linked correctly, or permissions not configured in Podfile for iOS.fixFor iOS, ensure `setup_permissions()` is called in your `Podfile` with the required permissions and run `pod install`. For Android, verify auto-linking or manual linking steps if necessary. Clean build caches and rebuild the app. -
Error: `TypeError: null is not an object (evaluating '_NativePermissions.request')`
cause Similar to the `check` error, indicates the native module isn't properly initialized or linked for the target platform.fixCheck your `Podfile` for `react-native-permissions/scripts/setup.rb` and `setup_permissions()`. Ensure `pod install` has been run. For Android, verify `MainApplication.java` has `new ReactNativePermissionsPackage()` if not using autolinking. Clear caches and rebuild. -
Could not find a declaration file for module 'react-native-permissions'. '/node_modules/react-native-permissions/lib/commonjs/index.js' implicitly has an 'any' type.
cause TypeScript cannot find type definitions for the module or `moduleResolution` is incorrect.fixEnsure `react-native-permissions` is installed correctly. For TypeScript, update `tsconfig.json` to include `"moduleResolution": "bundler"` (especially for v5.5.0+) or ensure types are correctly picked up by your IDE and bundler.
Warnings
- breaking Starting with v5.5.0, the package utilizes the `package.json` `exports` property for module resolution. This requires bundlers to be configured correctly.
- gotcha iOS permissions require explicit declaration in the `Podfile` and corresponding usage descriptions in `Info.plist`. Failing to do so will result in runtime crashes or permissions not being available.
- gotcha On Android, `check` might return `UNAVAILABLE` for unsupported permissions instead of `DENIED`. This was addressed in v5.4.2.
- gotcha When requesting location permissions on iOS 14 and above, granular `LocationAccuracy` (`LocationWhenInUse` or `LocationAlways`) can be requested. Using `CLLocationManager` directly for authorization status on older iOS versions might lead to incorrect behavior.
- gotcha Missing privacy usage descriptions in `Info.plist` (iOS) or `AndroidManifest.xml` (Android) for requested permissions will cause the app to crash or permissions to be silently denied by the OS.
Install
-
npm install react-native-permissions -
yarn add react-native-permissions -
pnpm add react-native-permissions
Imports
- request
import request from 'react-native-permissions';
import { request } from 'react-native-permissions'; - check
const { check } = require('react-native-permissions');import { check } from 'react-native-permissions'; - PERMISSIONS
import { PERMISSIONS } from 'react-native-permissions'; - RESULTS
import { RESULTS } from 'react-native-permissions'; - Platform
import { Platform } from 'react-native';
Quickstart
import { check, request, PERMISSIONS, RESULTS } from 'react-native-permissions';
import { Platform, Alert } from 'react-native';
const getCameraPermission = async () => {
let permission;
if (Platform.OS === 'ios') {
permission = PERMISSIONS.IOS.CAMERA;
} else if (Platform.OS === 'android') {
permission = PERMISSIONS.ANDROID.CAMERA;
} else {
Alert.alert('Unsupported platform');
return;
}
try {
let result = await check(permission);
if (result === RESULTS.DENIED) {
result = await request(permission);
}
if (result === RESULTS.GRANTED) {
Alert.alert('Camera permission granted!');
} else if (result === RESULTS.BLOCKED) {
Alert.alert('Camera permission blocked', 'Please go to settings to enable camera.');
} else {
Alert.alert('Camera permission status: ' + result);
}
} catch (error) {
console.error('Permission check failed', error);
Alert.alert('Error requesting permission');
}
};
// Call this function from a component or an event handler
// getCameraPermission();