![Create React App Officially Deprecated Amid React 19 Compatibility Issues](https://cdn.sanity.io/images/cgdhsj6q/production/04fa08cf844d798abc0e1a6391c129363cc7e2ab-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Create React App Officially Deprecated Amid React 19 Compatibility Issues
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.
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
The current supported permissions are:
###New in version 0.2.X
##General Usage
const Permissions = require('react-native-permissions');
//...
//check the status of a single permission
componentDidMount() {
Permissions.getPermissionStatus('photo')
.then(response => {
//response is one of: 'authorized', 'denied', 'restricted', or 'undetermined'
this.setState({ photoPermission: response })
});
}
//request permission to access photos
_requestPermission() {
Permissions.requestPermission('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.checkMultiplePermissions(['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.bind(this)}
: {text: 'Open Settings', onPress: Permissions.openSettings}
]
)
}
//...
##API
###Permission statuses Promises resolve into one of these statuses
Return value | Notes |
---|---|
authorized | user has authorized this permission |
denied | user has denied permissions at least once. On iOS this means that the user will not be prompted again. Android users can be promted multiple times until they select 'Never ask me again' |
restricted | iOS only |
undetermined | user has not yet been prompted with a permission dialog |
###Supported permission types
Name | iOS | Android |
---|---|---|
location | ✔️ | ✔ |
camera | ✔️ | ✔ |
microphone | ✔️ | ✔ |
photo | ✔️ | ✔ |
contacts | ✔️ | ✔ |
event | ✔️ | ✔ |
bluetooth | ✔️ | ❌ |
reminder | ✔️ | ❌ |
notification | ✔️ | ❌ |
backgroundRefresh | ✔️ | ❌ |
###Methods
Method Name | Arguments | Notes |
---|---|---|
getPermissionStatus | type | - Returns a promise with the permission status. Note: for type location , iOS AuthorizedAlways and AuthorizedWhenInUse both return authorized |
requestPermission | 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. Note: see below for special cases |
checkMultiplePermissions | [types] | - Accepts an array of permission types and returns a promise with an object mapping permission types to statuses |
getPermissionTypes | none | - Returns an array of valid permission types |
openSettings | none | - Switches the user to the settings page of your app (iOS 8.0 and later) |
canOpenSettings | none | - Returns a boolean indicating if the device supports switching to the settings page |
###iOS Notes
Permission type bluetooth
represents the status of the CBPeripheralManager
. Don't use this if only need CBCentralManager
requestPermission
also accepts a second parameter for types location
and notification
.
location
: the second parameter is a string, either always
or whenInUse
(default).notification
: the second parameter is an array with the desired alert types. Any combination of alert
, badge
and sound
(default requests all three)///example
Permissions.requestPermission('location', 'always')
.then(response => {
this.setState({ locationPermission: response })
})
Permissions.requestPermission('notification', ['alert', 'badge'])
.then(response => {
this.setState({ notificationPermission: response })
})
###Android Notes
All required permissions also need to be included in the Manifest before they can be requested. Otherwise requestPermission
will immediately return denied
.
Permissions are automatically accepted for targetSdkVersion < 23 but you can still use getPermissionStatus
to check if the user has disabled them from Settings.
Here's a map of types to Android system permissions names:
location
-> android.permission.ACCESS_FINE_LOCATION
camera
-> android.permission.CAMERA
microphone
-> android.permission.RECORD_AUDIO
photo
-> android.permission.READ_EXTERNAL_STORAGE
contacts
-> android.permission.READ_CONTACTS
event
-> android.permission.READ_CALENDAR
You can request write access to any of these types by also including the appropriate write permission in the Manifest. Read more here: https://developer.android.com/guide/topics/security/permissions.html#normal-dangerous
##Setup
npm install --save react-native-permissions
rnpm link
###Or manualy linking
####iOS
####Android #####Step 1 - Update Gradle Settings
// file: android/settings.gradle
...
include ':react-native-permissions'
project(':react-native-permissions').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-permissions/android')
#####Step 2 - Update Gradle Build
// file: android/app/build.gradle
...
dependencies {
...
compile project(':react-native-permissions')
}
#####Step 3 - Register React Package
...
import com.joshblour.reactnativepermissions.ReactNativePermissionsPackage; // <--- import
public class MainApplication extends Application implements ReactApplication {
...
@Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage(),
new ReactNativePermissionsPackage() // <------ add the package
);
}
...
}
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
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.