UNPKG

8.11 kBMarkdownView Raw
1# expo-permissions
2
3When it comes to adding functionality that can access potentially sensitive information on a user's device, such as their location, or possibly send them possibly unwanted push notifications, you will need to ask the user for their permission first. Unless you've already asked their permission, then no need. And so we have the `Permissions` module.
4
5If you are deploying your app to the Apple iTunes Store, you should consider adding additional metadata to your app in order to customize the system permissions dialog and explain why your app requires permissions. See more info in the [App Store Deployment Guide](https://docs.expo.io/versions/latest/guides/app-stores.html#system-permissions-dialogs-on-ios).
6
7## Installation
8
9*If your app is running in [Expo](https://expo.io) then everything is already set up for you, just `import { Permissions } from 'expo';`*
10
11Otherwise, you need to install the package from `npm` registry.
12
13`yarn add expo-permissions` or `npm install expo-permissions`
14
15Also, make sure that you have [expo-core](https://github.com/expo/expo-core) installed, as it is required by `expo-permissions` to work properly.
16
17### iOS
18
19Add these dependencies to your `Podfile`:
20
21```ruby
22pod 'EXPermissions', path: '../node_modules/expo-permissions/ios'
23pod 'EXPermissionsInterface', path: '../node_modules/expo-permissions-interface/ios'
24```
25
26and run `pod install` under the parent directory of your `Podfile`.
27
28### Android
29
301. Append the following lines to `android/settings.gradle`:
31 ```gradle
32 include ':expo-permissions'
33 project(':expo-permissions').projectDir = new File(rootProject.projectDir, '../node_modules/expo-permissions/android')
34 ```
35 and if not already included
36 ```gradle
37 include ':expo-permissions-interface'
38 project(':expo-permissions-interface').projectDir = new File(rootProject.projectDir, '../node_modules/expo-permissions-interface/android')
39 ```
402. Insert the following lines inside the dependencies block in `android/app/build.gradle`:
41 ```gradle
42 compile project(':expo-permissions')
43 ```
44 and if not already included
45 ```gradle
46 compile project(':expo-permissions-interface')
47 ```
483. Add `new PermissionsPackage()` to your module registry provider in `MainApplication.java`.
49
50## Methods
51
52### `Expo.Permissions.getAsync(...permissionTypes)`
53
54Determines whether your app has already been granted access to the provided permissions types.
55
56#### Arguments
57
58- **permissionTypes (_string_)** -- The names of the permissions types.
59
60#### Returns
61
62Returns a `Promise` that is resolved with the information about the permissions, including status, expiration and scope (if it applies to the permission type).
63Top-level `status` and `exprires` keys stores combined info of each component permission that is asked for.
64If any permission resulted in negative result than that negative result is propagated here, that means top-level values are positive only if all component values are positive.
65
66Examples `[...componentsValues] => topLevelStatus`:
67* `[granted, denied, granted] => denied`
68* `[granted, granted, granted] => granted`
69
70```javascript
71{
72 status, // combined status of all component permissions being asked for, if any of has status !== 'granted' then that status is propagated here
73 expires, // combined expires of all permissions being asked for, same as status
74 permissions: { // an object with an entry for each permission requested
75 [Permissions.TYPE]: {
76 status,
77 expires,
78 ... // any additional permission-specific fields
79 },
80 ...
81 },
82}
83```
84
85#### Example
86
87```javascript
88async function alertIfRemoteNotificationsDisabledAsync() {
89 const { Permissions } = Expo;
90 const { status } = await Permissions.getAsync(Permissions.NOTIFICATIONS);
91 if (status !== 'granted') {
92 alert('Hey! You might want to enable notifications for my app, they are good.');
93 }
94}
95
96async function checkMultiPermissions() {
97 const { Permissions } = Expo;
98 const { status, expires, permissions } = await Permissions.getAsync(Permissions.CALENDAR, Permissions.CONTACTS)
99 if (status !== 'granted') {
100 alert('Hey! You heve not enabled selected permissions');
101 }
102}
103```
104
105### `Expo.Permissions.askAsync(...types)`
106
107Prompt the user for types of permissions. If they have already granted access, response will be success.
108
109#### Arguments
110
111- **types (_string_)** -- The names of the permissions types.
112
113#### Returns
114
115Same as for `Permissions.getAsync`
116
117#### Example
118
119```javascript
120async function getLocationAsync() {
121 const { Location, Permissions } = Expo;
122 const { status } = await Permissions.askAsync(Permissions.LOCATION);
123 if (status === 'granted') {
124 return Location.getCurrentPositionAsync({enableHighAccuracy: true});
125 } else {
126 throw new Error('Location permission not granted');
127 }
128}
129```
130
131## Permissions types
132
133### `Permissions.NOTIFICATIONS`
134
135The permission type for user-facing notifications **and** remote push notifications.
136
137> **Note:** On iOS, asking for this permission asks the user not only for permission to register for push/remote notifications, but also for showing notifications as such. At the moment remote notifications will only be received when notifications are permitted to play a sound, change the app badge or be displayed as an alert. As iOS is more detailed when it comes to notifications permissions, this permission status will contain not only `status` and `expires`, but also Boolean values for `allowsSound`, `allowsAlert` and `allowsBadge`.
138
139> **Note:** On iOS, this does not disambiguate `undetermined` from `denied` and so will only ever return `granted` or `undetermined`. This is due to the way the underlying native API is implemented.
140
141> **Note:** Android does not differentiate between permissions for local and remote notifications, so status of permission for `NOTIFICATIONS` should always be the same as the status for `USER_FACING_NOTIFICATIONS`.
142
143### `Permissions.USER_FACING_NOTIFICATIONS`
144
145The permission type for user-facing notifications. This does **not** register your app to receive remote push notifications; see the `NOTIFICATIONS` permission.
146
147> **Note:** iOS provides more detailed permissions, so the permission status will contain not only `status` and `expires`, but also Boolean values for `allowsSound`, `allowsAlert` and `allowsBadge`.
148
149> **Note:** Android does not differentiate between permissions for local and remote notifications, so status of permission for `USER_FACING_NOTIFICATIONS` should always be the same as the status for `NOTIFICATIONS`.
150
151### `Permissions.LOCATION`
152
153The permission type for location access.
154
155### `Permissions.CAMERA`
156
157The permission type for photo and video taking.
158
159### `Permissions.AUDIO_RECORDING`
160
161The permission type for audio recording.
162
163### `Permissions.CONTACTS`
164
165The permission type for reading contacts.
166
167### `Permissions.CAMERA_ROLL`
168
169The permission type for reading or writing to the camera roll.
170
171### `Permissions.CALENDAR`
172
173The permission type for reading or writing to the calendar.
174
175### `Permissions.REMINDERS`
176
177The permission type for reading or writing reminders (iOS only).
178
179### `Expo.Permissions.SYSTEM_BRIGHTNESS`
180
181The permissions type for changing brighness of the screen
182
183## Android: permissions equivalents inside `app.json`
184
185If you specified `android.permissions` inside your `app.json` ([read more about configuration](https://docs.expo.io/versions/latest/workflow/configuration.html#android)) you have to use values corresponding to their `Expo.Permissions` equivalents.
186
187| Expo | Android |
188| --------------- | --------------------------------------------------|
189| LOCATION | ACCESS\_COARSE\_LOCATION, ACCESS\_FINE_LOCATION |
190| CAMERA | CAMERA |
191| AUDIO_RECORDING | RECORD_AUDIO |
192| CONTACTS | READ_CONTACTS |
193| CAMERA_ROLL | READ\_EXTERNAL\_STORAGE, WRITE\_EXTERNAL\_STORAGE |
194| CALENDAR | READ\_CALENDAR, WRITE\_CALENDAR |