|
@@ -1,18 +1,30 @@
|
1
|
1
|
# ☝🏼 React Native Permissions
|
2
|
2
|
|
|
3
|
+[![npm version](https://badge.fury.io/js/react-native-permissions.svg)](https://badge.fury.io/js/react-native-permissions)
|
|
4
|
+[![npm](https://img.shields.io/npm/dt/react-native-permissions.svg)](https://www.npmjs.org/package/react-native-permissions)
|
|
5
|
+![Platform - Android and iOS](https://img.shields.io/badge/platform-Android%20%7C%20iOS-yellow.svg)
|
|
6
|
+![MIT](https://img.shields.io/dub/l/vibe-d.svg)
|
|
7
|
+[![styled with prettier](https://img.shields.io/badge/styled_with-prettier-ff69b4.svg)](https://github.com/prettier/prettier)
|
|
8
|
+
|
3
|
9
|
Request user permissions from React Native, iOS + Android
|
4
|
10
|
|
5
|
11
|
| Version | React Native Support |
|
6
|
|
-|---|---|
|
7
|
|
-| 1.0.1 | 0.40.0 - 0.48.4 |
|
8
|
|
-| 0.2.5 | 0.33.0 - 0.39.0 |
|
9
|
|
-*Complies with [react-native-version-support-table](https://github.com/dangnelson/react-native-version-support-table)*
|
|
12
|
+| ------- | -------------------- |
|
|
13
|
+| 1.0.1 | 0.40.0 - 0.48.4 |
|
|
14
|
+| 0.2.5 | 0.33.0 - 0.39.0 |
|
|
15
|
+
|
|
16
|
+_Complies with
|
|
17
|
+[react-native-version-support-table](https://github.com/dangnelson/react-native-version-support-table)_
|
10
|
18
|
|
11
|
19
|
## ⚠️ Breaking changes in version 1.0.0
|
12
|
|
-- Now using React Native's own JS `PermissionsAndroid` module on Android, which is great because we no longer have to do any additional linking on Android
|
13
|
|
-- Updated API to be closer to React Native's `PermissionsAndroid`
|
14
|
|
-- Removed `openSettings()` support on Android (to stay linking-free). There are several NPM modules available for this
|
15
|
|
-- `restricted` status now supported on Android, although it means something different than iOS
|
|
20
|
+
|
|
21
|
+* Now using React Native's own JS `PermissionsAndroid` module on Android, which
|
|
22
|
+ is great because we no longer have to do any additional linking on Android
|
|
23
|
+* Updated API to be closer to React Native's `PermissionsAndroid`
|
|
24
|
+* Removed `openSettings()` support on Android (to stay linking-free). There are
|
|
25
|
+ several NPM modules available for this
|
|
26
|
+* `restricted` status now supported on Android, although it means something
|
|
27
|
+ different than iOS
|
16
|
28
|
|
17
|
29
|
## Setup
|
18
|
30
|
|
|
@@ -22,13 +34,15 @@ npm install --save react-native-permissions
|
22
|
34
|
yarn add react-native-permissions
|
23
|
35
|
```
|
24
|
36
|
|
25
|
|
-*📌 Don't forget to add permissions to `AndroidManifest.xml` for android and `Info.plist` for iOS (Xcode >= 8). See notes below for more details.*
|
|
37
|
+_📌 Don't forget to add permissions to `AndroidManifest.xml` for android and
|
|
38
|
+`Info.plist` for iOS (Xcode >= 8). See notes below for more details._
|
26
|
39
|
|
27
|
40
|
### Additional iOS setup
|
28
|
41
|
|
29
|
42
|
#### Using cocoaPods
|
30
|
43
|
|
31
|
|
-Update the following line with your path to `node_modules/` and add it to your podfile:
|
|
44
|
+Update the following line with your path to `node_modules/` and add it to your
|
|
45
|
+podfile:
|
32
|
46
|
|
33
|
47
|
```ruby
|
34
|
48
|
pod 'ReactNativePermissions', :path => '../node_modules/react-native-permissions'
|
|
@@ -42,12 +56,15 @@ react-native link react-native-permissions
|
42
|
56
|
|
43
|
57
|
#### Using manual linking
|
44
|
58
|
|
45
|
|
-1. In the XCode's "Project navigator", right click on your project's Libraries folder ➜ `Add Files to <...>`
|
46
|
|
-2. Go to `node_modules` ➜ `react-native-permissions` ➜ select `ReactNativePermissions.xcodeproj`
|
47
|
|
-3. Add `libReactNativePermissions.a` to `Build Phases` -> `Link Binary With Libraries`
|
|
59
|
+1. In the XCode's "Project navigator", right click on your project's Libraries
|
|
60
|
+ folder ➜ `Add Files to <...>`
|
|
61
|
+2. Go to `node_modules` ➜ `react-native-permissions` ➜ select
|
|
62
|
+ `ReactNativePermissions.xcodeproj`
|
|
63
|
+3. Add `libReactNativePermissions.a` to `Build Phases` -> `Link Binary With
|
|
64
|
+ Libraries`
|
48
|
65
|
|
49
|
66
|
```js
|
50
|
|
-import Permissions from "react-native-permissions";
|
|
67
|
+import Permissions from 'react-native-permissions'
|
51
|
68
|
|
52
|
69
|
//...
|
53
|
70
|
|
|
@@ -56,31 +73,31 @@ export default class extends React.Component {
|
56
|
73
|
|
57
|
74
|
// Check the status of a single permission
|
58
|
75
|
componentDidMount() {
|
59
|
|
- Permissions.check("photo").then(response => {
|
|
76
|
+ Permissions.check('photo').then(response => {
|
60
|
77
|
// Response is one of: 'authorized', 'denied', 'restricted', or 'undetermined'
|
61
|
|
- this.setState({ photoPermission: response });
|
62
|
|
- });
|
|
78
|
+ this.setState({ photoPermission: response })
|
|
79
|
+ })
|
63
|
80
|
}
|
64
|
81
|
|
65
|
82
|
// Request permission to access photos
|
66
|
83
|
_requestPermission = () => {
|
67
|
|
- Permissions.request("photo").then(response => {
|
|
84
|
+ Permissions.request('photo').then(response => {
|
68
|
85
|
// Returns once the user has chosen to 'allow' or to 'not allow' access
|
69
|
86
|
// Response is one of: 'authorized', 'denied', 'restricted', or 'undetermined'
|
70
|
|
- this.setState({ photoPermission: response });
|
71
|
|
- });
|
72
|
|
- };
|
|
87
|
+ this.setState({ photoPermission: response })
|
|
88
|
+ })
|
|
89
|
+ }
|
73
|
90
|
|
74
|
91
|
// Check the status of multiple permissions
|
75
|
92
|
_checkCameraAndPhotos = () => {
|
76
|
|
- Permissions.checkMultiple(["camera", "photo"]).then(response => {
|
|
93
|
+ Permissions.checkMultiple(['camera', 'photo']).then(response => {
|
77
|
94
|
//response is an object mapping type to permission
|
78
|
95
|
this.setState({
|
79
|
96
|
cameraPermission: response.camera,
|
80
|
|
- photoPermission: response.photo
|
81
|
|
- });
|
82
|
|
- });
|
83
|
|
- };
|
|
97
|
+ photoPermission: response.photo,
|
|
98
|
+ })
|
|
99
|
+ })
|
|
100
|
+ }
|
84
|
101
|
|
85
|
102
|
// This is a common pattern when asking for permissions.
|
86
|
103
|
// iOS only gives you once chance to show the permission dialog,
|
|
@@ -90,19 +107,19 @@ export default class extends React.Component {
|
90
|
107
|
// If the user already denied access, we can ask them to enable it from settings.
|
91
|
108
|
_alertForPhotosPermission() {
|
92
|
109
|
Alert.alert(
|
93
|
|
- "Can we access your photos?",
|
94
|
|
- "We need access so you can set your profile pic",
|
|
110
|
+ 'Can we access your photos?',
|
|
111
|
+ 'We need access so you can set your profile pic',
|
95
|
112
|
[
|
96
|
113
|
{
|
97
|
|
- text: "No way",
|
98
|
|
- onPress: () => console.log("Permission denied"),
|
99
|
|
- style: "cancel"
|
|
114
|
+ text: 'No way',
|
|
115
|
+ onPress: () => console.log('Permission denied'),
|
|
116
|
+ style: 'cancel',
|
100
|
117
|
},
|
101
|
|
- this.state.photoPermission == "undetermined"
|
102
|
|
- ? { text: "OK", onPress: this._requestPermission }
|
103
|
|
- : { text: "Open Settings", onPress: Permissions.openSettings }
|
104
|
|
- ]
|
105
|
|
- );
|
|
118
|
+ this.state.photoPermission == 'undetermined'
|
|
119
|
+ ? { text: 'OK', onPress: this._requestPermission }
|
|
120
|
+ : { text: 'Open Settings', onPress: Permissions.openSettings },
|
|
121
|
+ ],
|
|
122
|
+ )
|
106
|
123
|
}
|
107
|
124
|
|
108
|
125
|
//...
|
|
@@ -115,84 +132,92 @@ export default class extends React.Component {
|
115
|
132
|
|
116
|
133
|
Promises resolve into one of these statuses:
|
117
|
134
|
|
118
|
|
-| Return value | Notes|
|
119
|
|
-|---|---|
|
120
|
|
-|`authorized`| User has authorized this permission |
|
121
|
|
-|`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 promted multiple times until they select 'Never ask me again'|
|
122
|
|
-|`restricted`| **iOS only** - 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 |
|
123
|
|
-|`undetermined`| User has not yet been prompted with a permission dialog |
|
|
135
|
+| Return value | Notes |
|
|
136
|
+| -------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
137
|
+| `authorized` | User has authorized this permission |
|
|
138
|
+| `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 promted multiple times until they select 'Never ask me again' |
|
|
139
|
+| `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 |
|
|
140
|
+| `undetermined` | User has not yet been prompted with a permission dialog |
|
124
|
141
|
|
125
|
142
|
### Supported permissions types
|
126
|
143
|
|
127
|
144
|
The current supported permissions are:
|
128
|
145
|
|
129
|
|
-| | Type | iOS | Android |
|
130
|
|
-|---|---|---|---|
|
131
|
|
-| Location |`location`| ✔️ | ✔ |
|
132
|
|
-| Camera |`camera`| ✔️ | ✔ |
|
133
|
|
-| Microphone |`microphone`| ✔️ | ✔ |
|
134
|
|
-| Photos |`photo`| ✔️ | ✔ |
|
135
|
|
-| Contacts |`contacts`| ✔️ | ✔ |
|
136
|
|
-| Events |`event`| ✔️ | ✔ |
|
137
|
|
-| Bluetooth |`bluetooth`| ✔️ | ❌ |
|
138
|
|
-| Reminders |`reminder`| ✔️ | ❌ |
|
139
|
|
-| Push Notifications |`notification`| ✔️ | ❌ |
|
140
|
|
-| Background Refresh |`backgroundRefresh`| ✔️ | ❌ |
|
141
|
|
-| Speech Recognition |`speechRecognition`| ✔️ | ❌ |
|
142
|
|
-| Storage |`storage`| ❌️ | ✔ |
|
143
|
|
-| Phone Call |`callPhone`| ❌️ | ✔ |
|
144
|
|
-| Read SMS |`readSms`| ❌️ | ✔ |
|
145
|
|
-| Receive SMS |`receiveSms`| ❌️ | ✔ |
|
146
|
|
-
|
|
146
|
+| | Type | iOS | Android |
|
|
147
|
+| ------------------ | ------------------- | --- | ------- |
|
|
148
|
+| Location | `location` | ✔️ | ✔ |
|
|
149
|
+| Camera | `camera` | ✔️ | ✔ |
|
|
150
|
+| Microphone | `microphone` | ✔️ | ✔ |
|
|
151
|
+| Photos | `photo` | ✔️ | ✔ |
|
|
152
|
+| Contacts | `contacts` | ✔️ | ✔ |
|
|
153
|
+| Events | `event` | ✔️ | ✔ |
|
|
154
|
+| Bluetooth | `bluetooth` | ✔️ | ❌ |
|
|
155
|
+| Reminders | `reminder` | ✔️ | ❌ |
|
|
156
|
+| Push Notifications | `notification` | ✔️ | ❌ |
|
|
157
|
+| Background Refresh | `backgroundRefresh` | ✔️ | ❌ |
|
|
158
|
+| Speech Recognition | `speechRecognition` | ✔️ | ❌ |
|
|
159
|
+| Storage | `storage` | ❌️ | ✔ |
|
|
160
|
+| Phone Call | `callPhone` | ❌️ | ✔ |
|
|
161
|
+| Read SMS | `readSms` | ❌️ | ✔ |
|
|
162
|
+| Receive SMS | `receiveSms` | ❌️ | ✔ |
|
147
|
163
|
|
148
|
164
|
### Methods
|
149
|
165
|
|
150
|
|
-| Method Name | Arguments | Notes
|
151
|
|
-|---|---|---|
|
152
|
|
-| `check()` | `type` | - Returns a promise with the permission status. See iOS Notes for special cases |
|
153
|
|
-| `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|
|
154
|
|
-| `checkMultiple()` | `[types]` | - Accepts an array of permission types and returns a promise with an object mapping permission types to statuses |
|
155
|
|
-| `getTypes()` | *none* | - Returns an array of valid permission types |
|
156
|
|
-| `openSettings()` | *none* | - *(iOS only - 8.0 and later)* Switches the user to the settings page of your app |
|
157
|
|
-| `canOpenSettings()` | *none* | - *(iOS only)* Returns a boolean indicating if the device supports switching to the settings page |
|
|
166
|
+| Method Name | Arguments | Notes |
|
|
167
|
+| ------------------- | --------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
168
|
+| `check()` | `type` | - Returns a promise with the permission status. See iOS Notes for special cases |
|
|
169
|
+| `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 |
|
|
170
|
+| `checkMultiple()` | `[types]` | - Accepts an array of permission types and returns a promise with an object mapping permission types to statuses |
|
|
171
|
+| `getTypes()` | _none_ | - Returns an array of valid permission types |
|
|
172
|
+| `openSettings()` | _none_ | - _(iOS only - 8.0 and later)_ Switches the user to the settings page of your app |
|
|
173
|
+| `canOpenSettings()` | _none_ | - _(iOS only)_ Returns a boolean indicating if the device supports switching to the settings page |
|
158
|
174
|
|
159
|
175
|
### iOS Notes
|
160
|
176
|
|
161
|
|
-- Permission type `bluetooth` represents the status of the `CBPeripheralManager`. Don't use this if only need `CBCentralManager`
|
162
|
|
-- Permission type `location` accepts a second parameter for `request()` and `check()`;
|
163
|
|
-the second parameter is a string, either `always` or `whenInUse` (default).
|
164
|
|
-- Permission type `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).
|
|
177
|
+* Permission type `bluetooth` represents the status of the
|
|
178
|
+ `CBPeripheralManager`. Don't use this if only need `CBCentralManager`
|
|
179
|
+* Permission type `location` accepts a second parameter for `request()` and
|
|
180
|
+ `check()`; the second parameter is a string, either `always` or `whenInUse`
|
|
181
|
+ (default).
|
|
182
|
+* Permission type `notification` accepts a second parameter for `request()`. The
|
|
183
|
+ second parameter is an array with the desired alert types. Any combination of
|
|
184
|
+ `alert`, `badge` and `sound` (default requests all three).
|
165
|
185
|
|
166
|
186
|
```js
|
167
|
187
|
// example
|
168
|
|
-Permissions.check("location", "always").then(response => {
|
169
|
|
- this.setState({ locationPermission: response });
|
170
|
|
-});
|
|
188
|
+Permissions.check('location', 'always').then(response => {
|
|
189
|
+ this.setState({ locationPermission: response })
|
|
190
|
+})
|
171
|
191
|
|
172
|
|
-Permissions.request("location", "always").then(response => {
|
173
|
|
- this.setState({ locationPermission: response });
|
174
|
|
-});
|
|
192
|
+Permissions.request('location', 'always').then(response => {
|
|
193
|
+ this.setState({ locationPermission: response })
|
|
194
|
+})
|
175
|
195
|
|
176
|
|
-Permissions.request("notification", ["alert", "badge"]).then(response => {
|
177
|
|
- this.setState({ notificationPermission: response });
|
178
|
|
-});
|
|
196
|
+Permissions.request('notification', ['alert', 'badge']).then(response => {
|
|
197
|
+ this.setState({ notificationPermission: response })
|
|
198
|
+})
|
179
|
199
|
```
|
180
|
200
|
|
181
|
|
-- You cannot request microphone permissions on the simulator.
|
182
|
|
-- With Xcode 8, you now need to add usage descriptions for each permission you will request. Open Xcode ➜ `Info.plist` ➜ Add a key (starting with "Privacy - ...") with your kit specific permission.
|
|
201
|
+* You cannot request microphone permissions on the simulator.
|
|
202
|
+* With Xcode 8, you now need to add usage descriptions for each permission you
|
|
203
|
+ will request. Open Xcode ➜ `Info.plist` ➜ Add a key (starting with "Privacy -
|
|
204
|
+ ...") with your kit specific permission.
|
183
|
205
|
|
184
|
|
-Example:
|
185
|
|
-If you need Contacts permission you have to add the key `Privacy - Contacts Usage Description`.
|
|
206
|
+Example: If you need Contacts permission you have to add the key `Privacy -
|
|
207
|
+Contacts Usage Description`.
|
186
|
208
|
|
187
|
209
|
<img width="338" alt="3cde3b44-7ffd-11e6-918b-63888e33f983" src="https://cloud.githubusercontent.com/assets/1440796/18713019/271be540-8011-11e6-87fb-c3828c172dfc.png">
|
188
|
210
|
|
189
|
211
|
#### App Store submission disclaimer
|
190
|
212
|
|
191
|
|
-If you need to submit you 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**.
|
|
213
|
+If you need to submit you application to the AppStore, you need to add to your
|
|
214
|
+`Info.plist` all `*UsageDescription` keys with a string value explaining to the
|
|
215
|
+user how the app uses this data. **Even if you don't use them**.
|
192
|
216
|
|
193
|
|
-So before submitting your app to the App Store, make sure that in your `Info.plist` you have the following keys:
|
|
217
|
+So before submitting your app to the App Store, make sure that in your
|
|
218
|
+`Info.plist` you have the following keys:
|
194
|
219
|
|
195
|
|
-```
|
|
220
|
+```xml
|
196
|
221
|
<key>NSBluetoothPeripheralUsageDescription</key>
|
197
|
222
|
<string>Some description</string>
|
198
|
223
|
<key>NSCalendarsUsageDescription</key>
|
|
@@ -207,23 +232,52 @@ So before submitting your app to the App Store, make sure that in your `Info.pli
|
207
|
232
|
<string>Some description</string>
|
208
|
233
|
```
|
209
|
234
|
|
210
|
|
-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.
|
|
235
|
+This is required because during the phase of processing in the App Store
|
|
236
|
+submission, the system detects that you app contains code to request the
|
|
237
|
+permission `X` but don't have the `UsageDescription` key and then it rejects the
|
|
238
|
+build.
|
211
|
239
|
|
212
|
|
-> Please note that it will only be shown to the users the usage descriptions of the permissions you really require in your app.
|
|
240
|
+> Please note that it will only be shown to the users the usage descriptions of
|
|
241
|
+> the permissions you really require in your app.
|
213
|
242
|
|
214
|
243
|
You can find more informations about this issue in #46.
|
215
|
244
|
|
216
|
245
|
### Android Notes
|
217
|
246
|
|
218
|
|
-- Uses React Native's own [`PermissionsAndroid` JS API](http://facebook.github.io/react-native/docs/permissionsandroid.html).
|
219
|
|
-- All required permissions also need to be included in the `AndroidManifest.xml` file before they can be requested. Otherwise `request()` will immediately return `denied`.
|
220
|
|
-- Permissions are automatically accepted for **targetSdkVersion < 23** but you can still use `check()` to check if the user has disabled them from Settings.
|
221
|
|
-- You can request write access to any of these types by also including the appropriate write permission in the `AndroidManifest.xml` file. Read more [here](https://developer.android.com/guide/topics/security/permissions.html#normal-dangerous).
|
|
247
|
+* Uses React Native's own
|
|
248
|
+ [`PermissionsAndroid` JS API](http://facebook.github.io/react-native/docs/permissionsandroid.html).
|
|
249
|
+* All required permissions also need to be included in the `AndroidManifest.xml`
|
|
250
|
+ file before they can be requested. Otherwise `request()` will immediately
|
|
251
|
+ return `denied`.
|
|
252
|
+* You can request write access to any of these types by also including the
|
|
253
|
+ appropriate write permission in the `AndroidManifest.xml` file. Read more
|
|
254
|
+ [here](https://developer.android.com/guide/topics/security/permissions.html#normal-dangerous).
|
|
255
|
+* Permissions are automatically accepted for **targetSdkVersion < 23** but you
|
|
256
|
+ can still use `check()` to check if the user has disabled them from Settings.
|
|
257
|
+
|
|
258
|
+You might need to elevate the **targetSdkVersion** version in your
|
|
259
|
+`build.gradle`:
|
|
260
|
+
|
|
261
|
+```groovy
|
|
262
|
+android {
|
|
263
|
+ compileSdkVersion 23 // ← set at least 23
|
|
264
|
+ buildToolsVersion "23.0.1" // ← set at least 23.0.0
|
|
265
|
+
|
|
266
|
+ defaultConfig {
|
|
267
|
+ minSdkVersion 16
|
|
268
|
+ targetSdkVersion 23 // ← set at least 23
|
|
269
|
+ // ...
|
|
270
|
+```
|
222
|
271
|
|
223
|
272
|
## Troubleshooting
|
224
|
273
|
|
225
|
274
|
#### Q: iOS - App crashes as soon as I request permission
|
226
|
|
-> A: Starting with Xcode 8, you need to add permission descriptions. See iOS notes for more details. Thanks to [@jesperlndk](https://github.com/jesperlndk) for discovering this.
|
|
275
|
+
|
|
276
|
+> A: Starting with Xcode 8, you need to add permission descriptions. See iOS
|
|
277
|
+> notes for more details. Thanks to [@jesperlndk](https://github.com/jesperlndk)
|
|
278
|
+> for discovering this.
|
227
|
279
|
|
228
|
280
|
#### Q: iOS - App crashes when I change permission from settings
|
229
|
|
-> A: This is normal. iOS restarts your app when your privacy settings change. Just google "iOS crash permission change"
|
|
281
|
+
|
|
282
|
+> A: This is normal. iOS restarts your app when your privacy settings change.
|
|
283
|
+> Just google "iOS crash permission change"
|