Security News
require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
react-native-permissions
Advanced tools
The react-native-permissions package is a library for handling permissions in React Native applications. It provides a unified API to request and check permissions for various device features such as location, camera, microphone, and more.
Requesting Permissions
This feature allows you to request specific permissions from the user. In this example, the code requests camera permission on an Android device.
import { request, PERMISSIONS } from 'react-native-permissions';
async function requestCameraPermission() {
const result = await request(PERMISSIONS.ANDROID.CAMERA);
console.log(result);
}
Checking Permissions
This feature allows you to check the current status of a specific permission. The example checks if the camera permission is granted on an Android device.
import { check, PERMISSIONS, RESULTS } from 'react-native-permissions';
async function checkCameraPermission() {
const result = await check(PERMISSIONS.ANDROID.CAMERA);
if (result === RESULTS.GRANTED) {
console.log('Camera permission granted');
} else {
console.log('Camera permission not granted');
}
}
Open App Settings
This feature allows you to open the app settings so the user can manually grant permissions. The example demonstrates how to open the app settings.
import { openSettings } from 'react-native-permissions';
function openAppSettings() {
openSettings().catch(() => console.warn('Cannot open settings'));
}
The react-native-location package provides location services for React Native applications. It allows you to request and check location permissions, as well as get the current location of the device. Compared to react-native-permissions, it is more focused on location-specific functionalities.
The react-native-geolocation-service package is another library for handling geolocation in React Native apps. It offers more accurate and reliable location services compared to the default React Native geolocation API. While it focuses on geolocation, react-native-permissions provides a broader range of permission management.
The react-native-contacts package allows you to access and manage the device's contact list. It includes functionalities for requesting contact permissions, reading contacts, and adding new contacts. This package is specialized for contact management, whereas react-native-permissions covers a wider array of permissions.
Request user permissions from React Native, iOS + Android
Version | React Native Support |
---|---|
1.2.0+ | 0.52.0+ |
npm install --save react-native-permissions @react-native-community/async-storage
# --- or ---
yarn add react-native-permissions @react-native-community/async-storage
β οΈ To install @react-native-community/async-storage
, please refers to the package documentation.
π Don't forget to add permissions to AndroidManifest.xml
for android and Info.plist
for iOS (Xcode >= 8). See iOS Notes or Android Notes for more details.
Update the following line with your path to node_modules/
and add it to your podfile:
pod 'ReactNativePermissions', :path => '../node_modules/react-native-permissions'
react-native link react-native-permissions
Add Files to <β¦>
node_modules
β react-native-permissions
β select ReactNativePermissions.xcodeproj
libReactNativePermissions.a
to Build Phases
-> Link Binary With Libraries
import Permissions from 'react-native-permissions';
// OR const Permissions = require('react-native-permissions').default
// if you use CommonJS module system
// β¦
export default class extends React.Component {
// β¦
// Check the status of a single permission
componentDidMount() {
Permissions.check('photo').then(response => {
// Response is one of: 'authorized', 'denied', 'restricted', or 'undetermined'
this.setState({photoPermission: response});
});
}
// Request permission to access photos
_requestPermission = () => {
Permissions.request('photo').then(response => {
// Returns once the user has chosen to 'allow' or to 'not allow' access
// Response is one of: 'authorized', 'denied', 'restricted', or 'undetermined'
this.setState({photoPermission: response});
});
};
// Check the status of multiple permissions
_checkCameraAndPhotos = () => {
Permissions.checkMultiple(['camera', 'photo']).then(response => {
//response is an object mapping type to permission
this.setState({
cameraPermission: response.camera,
photoPermission: response.photo,
});
});
};
// This is a common pattern when asking for permissions.
// iOS only gives you once chance to show the permission dialog,
// after which the user needs to manually enable them from settings.
// The idea here is to explain why we need access and determine if
// the user will say no, so that we don't blow our one chance.
// If the user already denied access, we can ask them to enable it from settings.
_alertForPhotosPermission() {
Alert.alert(
'Can we access your photos?',
'We need access so you can set your profile pic',
[
{
text: 'No way',
onPress: () => console.log('Permission denied'),
style: 'cancel',
},
this.state.photoPermission == 'undetermined'
? {text: 'OK', onPress: this._requestPermission}
: {text: 'Open Settings', onPress: Permissions.openSettings},
],
);
}
// β¦
}
Promises resolve into one of these statuses:
Return value | Notes |
---|---|
authorized | User has authorized this permission |
denied | User has denied this permission at least once. On iOS this means that the user will not be prompted again. Android users can be prompted multiple times until they select 'Never ask me again' |
restricted | iOS - this means user is not able to grant this permission, either because it's not supported by the device or because it has been blocked by parental controls. Android - this means that the user has selected 'Never ask me again' while denying permission |
undetermined | User has not yet been prompted with a permission dialog |
The current supported permissions are:
Type | iOS | Android | |
---|---|---|---|
Camera | camera | βοΈ | β |
Contacts | contacts | βοΈ | β |
Events | event | βοΈ | β |
Location | location | βοΈ | β |
Microphone | microphone | βοΈ | β |
Photos | photo | βοΈ | β |
Background Refresh | backgroundRefresh | βοΈ | β |
Bluetooth | bluetooth | βοΈ | β |
Media Library | mediaLibrary | βοΈ | β |
Motion Activity | motion | βοΈ | β |
Push Notifications | notification | βοΈ | β |
Reminders | reminder | βοΈ | β |
Speech Recognition | speechRecognition | βοΈ | β |
Coarse location | coarseLocation | β | β |
Phone Call | callPhone | βοΈ | β |
Read SMS | readSms | βοΈ | β |
Receive SMS | receiveSms | βοΈ | β |
Send SMS | sendSms | βοΈ | β |
Storage | storage | βοΈ | β |
Method Name | Arguments | Notes |
---|---|---|
check() | type | - Returns a promise with the permission status. See iOS Notes for special cases |
request() | type | - Accepts any permission type except backgroundRefresh . If the current status is undetermined , shows the permission dialog and returns a promise with the resulting status. Otherwise, immediately return a promise with the current status. See iOS Notes for special cases |
checkMultiple() | [types] | - Accepts an array of permission types and returns a promise with an object mapping permission types to statuses |
getTypes() | none | - Returns an array of valid permission types |
openSettings() | none | - (iOS only - 8.0 and later) Switches the user to the settings page of your app |
canOpenSettings() | none | - (iOS only) Returns a boolean indicating if the device supports switching to the settings page |
bluetooth
represents the status of the CBPeripheralManager
. Don't use this if you only need CBCentralManager
.location
accepts a second parameter for request()
and check()
; the second parameter is a string, either always
or whenInUse
(default).notification
accepts a second parameter for request()
. The second parameter is an array with the desired alert types. Any combination of alert
, badge
and sound
(default requests all three).// example
Permissions.check('location', {type: 'always'}).then(response => {
this.setState({locationPermission: response});
});
Permissions.request('location', {type: 'always'}).then(response => {
this.setState({locationPermission: response});
});
Permissions.request('notification', {type: ['alert', 'badge']}).then(
response => {
this.setState({notificationPermission: response});
},
);
Info.plist
β Add a key (starting with "Privacy - β¦") with your kit specific permission.Example: If you need Contacts permission you have to add the key Privacy - Contacts Usage Description
.
If you need to submit your application to the AppStore, you need to add to your Info.plist
all *UsageDescription
keys with a string value explaining to the user how the app uses this data. Even if you don't use them.
So before submitting your app to the App Store, make sure that in your Info.plist
you have the following keys:
<key>NSBluetoothAlwaysUsageDescription</key>
<string>Some description</string>
<!-- only need peripheral if target is less than iOS 13 -->
<!-- https://developer.apple.com/documentation/bundleresources/information_property_list/nsbluetoothperipheralusagedescription -->
<key>NSBluetoothPeripheralUsageDescription</key>
<string>Some description</string>
<key>NSCalendarsUsageDescription</key>
<string>Some description</string>
<key>NSCameraUsageDescription</key>
<string>Some description</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>Some description</string>
<key>NSPhotoLibraryAddUsageDescription</key>
<string>Some description</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>Some description</string>
<key>NSSpeechRecognitionUsageDescription</key>
<string>Some description</string>
<key>NSAppleMusicUsageDescription</key>
<string>Some description</string>
<key>NSMotionUsageDescription</key>
<string>Some description</string>
This is required because during the phase of processing in the App Store submission, the system detects that you app contains code to request the permission X
but don't have the UsageDescription
key and then it rejects the build.
Please note that it will only be shown to the users the usage descriptions of the permissions you really require in your app.
You can find more information about this issue in #46.
PermissionsAndroid
JS API.AndroidManifest.xml
file before they can be requested. Otherwise request()
will immediately return denied
.AndroidManifest.xml
file. here. e.g. <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
Read more here.
// example
Permissions.request('camera', {
rationale: {
title: 'Cool Photo App Camera Permission',
message:
'Cool Photo App needs access to your camera ' +
'so you can take awesome pictures.',
},
}).then(response => {
this.setState({cameraPermission: response});
});
check()
to check if the user has disabled them from Settings.A: Starting with Xcode 8, you need to add permission descriptions. See iOS notes for more details. Thanks to @jesperlndk for discovering this.
A: This is normal. iOS restarts your app when your privacy settings change. Just google "iOS crash permission change".
FAQs
An unified permissions API for React Native on iOS, Android and Windows
The npm package react-native-permissions receives a total of 353,299 weekly downloads. As such, react-native-permissions popularity was classified as popular.
We found that react-native-permissions demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago.Β It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenanceβdevelopers should switch to Vite or other modern alternatives.