Skip to content

Using React Native Permissions v2

Brett Weissbart edited this page Oct 21, 2020 · 2 revisions

Version 1 of React Native Permissions includes every iOS permission out of the box, requiring you to add usage descriptions for each permission even if you don't use it. Version 2 breaks permissions into separate pods, allowing you to only support permissions you actually wish to use in the app. Flagship 10 adds support for permissions v2 and also provides helpers to add the appropriate pods, usage descriptions, and Android Manifest permissions.

1. Update Dependency

Update your react-native-permissions dependency in package.json to the latest available version of v2:

"react-native-permissions": "^2.0.0"

You can remove the dependency for @types/react-native-permissions as the TypeScript definitions are now contained within the main package.

2. Add New Config Property

Flagship adds a new config property called "permissions". An example of the format is as follows:

"permissions": {
  "ios": {
    LOCATION_WHEN_IN_USE: "Usage description for location when in use",
    REMINDERS: "Usage description for reminders"
  },
  "android": [
    ACCESS_FINE_LOCATION,
    CAMERA
  ]
}

The full list of permissions keys available for iOS and Android is as follows:

iOS

APP_TRACKING_TRANSPARENCY
BLUETOOTH_PERIPHERAL
CALENDARS
CAMERA
CONTACTS
FACE_ID
LOCATION_ALWAYS
LOCATION_WHEN_IN_USE
MEDIA_LIBRARY
MICROPHONE
MOTION
NOTIFICATIONS
PHOTO_LIBRARY
REMINDERS
SIRI
SPEECH_RECOGNITION
STOREKIT

Android

ACCEPT_HANDOVER
ACCESS_BACKGROUND_LOCATION
ACCESS_COARSE_LOCATION
ACCESS_FINE_LOCATION
ACTIVITY_RECOGNITION
ADD_VOICEMAIL
ANSWER_PHONE_CALLS
BODY_SENSORS
CALL_PHONE
CAMERA
GET_ACCOUNTS
PROCESS_OUTGOING_CALLS
READ_CALENDAR
READ_CALL_LOG
READ_CONTACTS
READ_EXTERNAL_STORAGE
READ_PHONE_NUMBERS
READ_PHONE_STATE
READ_SMS
RECEIVE_MMS
RECEIVE_SMS
RECEIVE_WAP_PUSH
RECORD_AUDIO
SEND_SMS
USE_SIP
WRITE_CALENDAR
WRITE_CALL_LOG
WRITE_CONTACTS
WRITE_EXTERNAL_STORAGE

3. Remove Duplicate Usage Descriptions

Review your configuration files for instances of usageDescriptionIOS. Remove any entries for permission-related values such as NSAppleMusicUsageDescription and NSCameraUsageDescription as these will be added automatically by Flagship when you opt-into these permissions via the permissions config.

Permissions v2 automatically handles the following usage description types:

NSAppleMusicUsageDescription
NSBluetoothAlwaysUsageDescription
NSBluetoothPeripheralUsageDescription
NSCalendarsUsageDescription
NSCameraUsageDescription
NSContactsUsageDescription
NSFaceIDUsageDescription
NSLocationAlwaysAndWhenInUseUsageDescription
NSLocationAlwaysUsageDescription
NSLocationWhenInUseUsageDescription
NSMicrophoneUsageDescription
NSMotionUsageDescription
NSPhotoLibraryUsageDescription
NSRemindersUsageDescription
NSSpeechRecognitionUsageDescription
NSSiriUsageDescription
NSUserTrackingUsageDescription

4. Update Code

Permissions v2 has an updated API which is described in their README: https://github.com/react-native-community/react-native-permissions#api

Note that you must now request/check iOS and Android permissions separately. Also note that they updated the constants for the return values, including removing UKNOWKN. In v2, UNAVAILABLE means that the user's device doesn't support the permission, DENIED means that the permission is not enabled but can be requested, GRANTED means that a permission has been enabled, and BLOCKED means that a user has to manually re-enable the permission in their settings.