Mathieu Acthernoene 7 years ago
parent
commit
1518fa4df1
1 changed files with 143 additions and 152 deletions
  1. 143
    152
      README.md

+ 143
- 152
README.md View File

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