Browse Source

Merge pull request #192 from axsann/master

Add support for permission request rationale. (resolve conflicts)
Mathieu Acthernoene 7 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
 
187
 
188
 ```js
188
 ```js
189
 // example
189
 // example
190
-Permissions.check('location', 'always').then(response => {
190
+Permissions.check('location', { type: 'always' }).then(response => {
191
   this.setState({ locationPermission: response })
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
   this.setState({ locationPermission: response })
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
 * You cannot request microphone permissions on the simulator.
205
 * You cannot request microphone permissions on the simulator.
254
 * You can request write access to any of these types by also including the
256
 * You can request write access to any of these types by also including the
255
   appropriate write permission in the `AndroidManifest.xml` file. Read more
257
   appropriate write permission in the `AndroidManifest.xml` file. Read more
256
   [here](https://developer.android.com/guide/topics/security/permissions.html#normal-dangerous).
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
 * Permissions are automatically accepted for **targetSdkVersion < 23** but you
276
 * Permissions are automatically accepted for **targetSdkVersion < 23** but you
258
   can still use `check()` to check if the user has disabled them from Settings.
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
     })
61
     })
62
   }
62
   }
63
 
63
 
64
-  request = permission => {
64
+  request = (permission, options) => {
65
     const androidPermission = permissionTypes[permission]
65
     const androidPermission = permissionTypes[permission]
66
 
66
 
67
     if (!androidPermission) {
67
     if (!androidPermission) {
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
   checkMultiple = permissions =>
93
   checkMultiple = permissions =>

+ 11
- 1
index.ios.js View File

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
     if (!permissionTypes.includes(permission)) {
56
     if (!permissionTypes.includes(permission)) {
47
       return Promise.reject(
57
       return Promise.reject(
48
         `ReactNativePermissions: ${
58
         `ReactNativePermissions: ${