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