Browse Source

Merge pull request #192 from axsann/master

Add support for permission request rationale. (resolve conflicts)
Mathieu Acthernoene 6 years ago
parent
commit
4243f704e2
No account linked to committer's email address
3 changed files with 51 additions and 15 deletions
  1. 24
    5
      README.md
  2. 16
    9
      index.android.js
  3. 11
    1
      index.ios.js

+ 24
- 5
README.md View File

@@ -187,17 +187,19 @@ The current supported permissions are:
187 187
 
188 188
 ```js
189 189
 // example
190
-Permissions.check('location', 'always').then(response => {
190
+Permissions.check('location', { type: 'always' }).then(response => {
191 191
   this.setState({ locationPermission: response })
192 192
 })
193 193
 
194
-Permissions.request('location', 'always').then(response => {
194
+Permissions.request('location', { type: 'always' }).then(response => {
195 195
   this.setState({ locationPermission: response })
196 196
 })
197 197
 
198
-Permissions.request('notification', ['alert', 'badge']).then(response => {
199
-  this.setState({ notificationPermission: response })
200
-})
198
+Permissions.request('notification', { type: ['alert', 'badge'] }).then(
199
+  response => {
200
+    this.setState({ notificationPermission: response })
201
+  },
202
+)
201 203
 ```
202 204
 
203 205
 * You cannot request microphone permissions on the simulator.
@@ -254,6 +256,23 @@ You can find more informations about this issue in #46.
254 256
 * You can request write access to any of these types by also including the
255 257
   appropriate write permission in the `AndroidManifest.xml` file. Read more
256 258
   [here](https://developer.android.com/guide/topics/security/permissions.html#normal-dangerous).
259
+
260
+* The optional rationale argument will show a dialog prompt.
261
+
262
+```js
263
+// example
264
+Permissions.request('camera', {
265
+  rationale: {
266
+    title: 'Cool Photo App Camera Permission',
267
+    message:
268
+      'Cool Photo App needs access to your camera ' +
269
+      'so you can take awesome pictures.',
270
+  },
271
+}).then(response => {
272
+  this.setState({ cameraPermission: response })
273
+})
274
+```
275
+
257 276
 * Permissions are automatically accepted for **targetSdkVersion < 23** but you
258 277
   can still use `check()` to check if the user has disabled them from Settings.
259 278
 

+ 16
- 9
index.android.js View File

@@ -61,7 +61,7 @@ class ReactNativePermissions {
61 61
     })
62 62
   }
63 63
 
64
-  request = permission => {
64
+  request = (permission, options) => {
65 65
     const androidPermission = permissionTypes[permission]
66 66
 
67 67
     if (!androidPermission) {
@@ -72,15 +72,22 @@ class ReactNativePermissions {
72 72
       )
73 73
     }
74 74
 
75
-    return PermissionsAndroid.request(androidPermission).then(result => {
76
-      // PermissionsAndroid.request() to native module resolves to boolean
77
-      // rather than string if running on OS version prior to Android M
78
-      if (typeof result === 'boolean') {
79
-        return result ? 'authorized' : 'denied'
80
-      }
75
+    let rationale = null
76
+    if (options != null) {
77
+      rationale = options.rationale
78
+    }
81 79
 
82
-      return setDidAskOnce(permission).then(() => RESULTS[result])
83
-    })
80
+    return PermissionsAndroid.request(androidPermission, rationale).then(
81
+      result => {
82
+        // PermissionsAndroid.request() to native module resolves to boolean
83
+        // rather than string if running on OS version prior to Android M
84
+        if (typeof result === 'boolean') {
85
+          return result ? 'authorized' : 'denied'
86
+        }
87
+
88
+        return setDidAskOnce(permission).then(() => RESULTS[result])
89
+      },
90
+    )
84 91
   }
85 92
 
86 93
   checkMultiple = permissions =>

+ 11
- 1
index.ios.js View File

@@ -42,7 +42,17 @@ class ReactNativePermissions {
42 42
     )
43 43
   }
44 44
 
45
-  request = (permission, type) => {
45
+  request = (permission, options) => {
46
+    let type = null
47
+    if (typeof options === 'string' || options instanceof Array) {
48
+      console.warn(
49
+        '[react-native-permissions] : You are using a deprecated version of request(). You should use an object as second parameter. Please check the documentation for more information : https://github.com/yonahforst/react-native-permissions',
50
+      )
51
+      type = options
52
+    } else if (options != null) {
53
+      type = options.type
54
+    }
55
+
46 56
     if (!permissionTypes.includes(permission)) {
47 57
       return Promise.reject(
48 58
         `ReactNativePermissions: ${