Mathieu Acthernoene f3d950098d Improve request type doc | 5 anos atrás | |
---|---|---|
.github | 5 anos atrás | |
android | 5 anos atrás | |
example | 5 anos atrás | |
ios | 5 anos atrás | |
.editorconfig | 5 anos atrás | |
.flowconfig | 5 anos atrás | |
.gitignore | 5 anos atrás | |
.npmignore | 7 anos atrás | |
.prettierignore | 5 anos atrás | |
.prettierrc | 5 anos atrás | |
LICENSE | 8 anos atrás | |
README.md | 5 anos atrás | |
RNPermissions.podspec | 5 anos atrás | |
index.js | 5 anos atrás | |
package.json | 5 anos atrás | |
yarn.lock | 5 anos atrás |
Request user permissions on iOS and Android.
version | react-native version |
---|---|
2.0.0+ | 0.56.0+ |
1.1.1 | 0.40.0 - 0.52.2 |
$ npm install --save react-native-permissions
# --- or ---
$ yarn add react-native-permissions
To allow installation of the needed permission handlers for your project (and only them), react-native-permissions
uses CocoaPods. Update the following line with your path to node_modules/
and add it to your podfile:
target 'YourAwesomeProject' do
# …
pod 'RNPermissions', :path => '../node_modules/react-native-permissions', :subspecs => [
'Core',
## Uncomment needed permissions
# 'BluetoothPeripheral',
# 'Calendars',
# 'Camera',
# 'Contacts',
# 'FaceID',
# 'LocationAlways',
# 'LocationWhenInUse',
# 'MediaLibrary',
# 'Microphone',
# 'Motion',
# 'Notifications',
# 'PhotoLibrary',
# 'Reminders',
# 'Siri',
# 'SpeechRecognition',
# 'StoreKit',
]
end
android/settings.gradle
:include ':react-native-permissions'
project(':react-native-permissions').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-permissions/android')
android/app/build.gradle
:dependencies {
// ...
implementation project(':react-native-permissions')
}
MainApplication.java
:import com.yonahforst.rnpermissions.RNPermissionsPackage; // <-- Add the import
public class MainApplication extends Application implements ReactApplication {
// …
@Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage(),
// …
new RNPermissionsPackage() // <-- Add it to the packages list
);
}
// …
}
📌 Don’t forget to add permissions to AndroidManifest.xml
for android and
Info.plist
for iOS (Xcode >= 8).
Promises resolve into one of these statuses:
Return value | Notes |
---|---|
RESULTS.UNAVAILABLE |
This feature is not available on this device. |
RESULTS.GRANTED |
The permission is granted. |
RESULTS.DENIED |
The permission has not been requested / is denied but requestable. |
RESULTS.NEVER_ASK_AGAIN |
The permission is not requestable anymore. |
import { ANDROID_PERMISSIONS, IOS_PERMISSIONS } from "react-native-permissions";
// For Android
// same as PermissionsAndroid
ANDROID_PERMISSIONS.READ_CALENDAR;
ANDROID_PERMISSIONS.WRITE_CALENDAR;
ANDROID_PERMISSIONS.CAMERA;
ANDROID_PERMISSIONS.READ_CONTACTS;
ANDROID_PERMISSIONS.WRITE_CONTACTS;
ANDROID_PERMISSIONS.GET_ACCOUNTS;
ANDROID_PERMISSIONS.ACCESS_FINE_LOCATION;
ANDROID_PERMISSIONS.ACCESS_COARSE_LOCATION;
ANDROID_PERMISSIONS.RECORD_AUDIO;
ANDROID_PERMISSIONS.READ_PHONE_STATE;
ANDROID_PERMISSIONS.CALL_PHONE;
ANDROID_PERMISSIONS.READ_CALL_LOG;
ANDROID_PERMISSIONS.WRITE_CALL_LOG;
ANDROID_PERMISSIONS.ADD_VOICEMAIL;
ANDROID_PERMISSIONS.USE_SIP;
ANDROID_PERMISSIONS.PROCESS_OUTGOING_CALLS;
ANDROID_PERMISSIONS.BODY_SENSORS;
ANDROID_PERMISSIONS.SEND_SMS;
ANDROID_PERMISSIONS.RECEIVE_SMS;
ANDROID_PERMISSIONS.READ_SMS;
ANDROID_PERMISSIONS.RECEIVE_WAP_PUSH;
ANDROID_PERMISSIONS.RECEIVE_MMS;
ANDROID_PERMISSIONS.READ_EXTERNAL_STORAGE;
ANDROID_PERMISSIONS.WRITE_EXTERNAL_STORAGE;
// new ones
ANDROID_PERMISSIONS.ANSWER_PHONE_CALLS;
ANDROID_PERMISSIONS.ACCEPT_HANDOVER;
ANDROID_PERMISSIONS.READ_PHONE_NUMBERS;
// For iOS
IOS_PERMISSIONS.BLUETOOTH_PERIPHERAL;
IOS_PERMISSIONS.CALENDARS;
IOS_PERMISSIONS.CAMERA;
IOS_PERMISSIONS.CONTACTS;
IOS_PERMISSIONS.FACE_ID;
IOS_PERMISSIONS.LOCATION_ALWAYS;
IOS_PERMISSIONS.LOCATION_WHEN_IN_USE;
IOS_PERMISSIONS.MEDIA_LIBRARY;
IOS_PERMISSIONS.MICROPHONE;
IOS_PERMISSIONS.MOTION;
IOS_PERMISSIONS.NOTIFICATIONS;
IOS_PERMISSIONS.PHOTO_LIBRARY;
IOS_PERMISSIONS.REMINDERS;
IOS_PERMISSIONS.SIRI;
IOS_PERMISSIONS.SPEECH_RECOGNITION;
IOS_PERMISSIONS.STOREKIT;
types used in usage examples
type Permission = keyof ANDROID_PERMISSIONS | keyof IOS_PERMISSIONS;
type PermissionStatus =
| "granted"
| "denied"
| "never_ask_again"
| "unavailable";
Check one permission status.
function check(permission: Permission): Promise<PermissionStatus>;
import { check, IOS_PERMISSIONS, RESULTS } from "react-native-permissions";
check(RNPermissions.IOS_PERMISSIONS.LOCATION_ALWAYS).then(result => {
switch (result) {
case RESULTS.UNAVAILABLE:
console.log("the feature is not available on this device");
break;
case RESULTS.GRANTED:
console.log("permission is granted");
break;
case RESULTS.DENIED:
console.log("permission is denied, but requestable");
break;
case RESULTS.NEVER_ASK_AGAIN:
console.log("permission is denied and not requestable");
break;
}
});
Check multiples permissions.
function checkMultiple<P: Permission>(permissions: P[]): Promise<{ [permission: P]: PermissionStatus }>;
import { checkMultiple, IOS_PERMISSIONS } from "react-native-permissions";
checkMultiple([
IOS_PERMISSIONS.LOCATION_ALWAYS,
IOS_PERMISSIONS.MEDIA_LIBRARY,
]).then(results => {
// results.LOCATION_ALWAYS
// results.MEDIA_LIBRARY
});
Request one permission.
type NotificationOption =
| "badge"
| "sound"
| "alert"
| "carPlay"
| "criticalAlert"
| "provisional";
type Rationale = {
title: string;
message: string;
buttonPositive: string;
buttonNegative?: string;
buttonNeutral?: string;
};
function request(
permission: string,
config: {
notificationOptions?: NotificationOption[];
rationale?: Rationale;
} = {},
): Promise<PermissionStatus>;
import { request, IOS_PERMISSIONS } from "react-native-permissions";
request(IOS_PERMISSIONS.LOCATION_ALWAYS).then(result => {
// …
});
Request multiples permissions.
function requestMultiple<P: Permission>(permissions: P[]): Promise<{ [permission: P]: PermissionStatus }>;
import { requestMultiple, IOS_PERMISSIONS } from "react-native-permissions";
requestMultiple([
IOS_PERMISSIONS.LOCATION_ALWAYS,
IOS_PERMISSIONS.MEDIA_LIBRARY,
]).then(results => {
// results.LOCATION_ALWAYS
// results.MEDIA_LIBRARY
});
Open application settings.
function openSettings(): Promise<boolean>;
import { openSettings } from "react-native-permissions";
openSettings().catch(() => console.warn("cannot open settings");
BLUETOOTH_PERIPHERAL
represents the status of the CBPeripheralManager
.notificationOptions
config array is omitted on NOTIFICATIONS
request, it will request alert
, badge
and sound
.