Skip to content

Commit cdebe08

Browse files
committed
2 parents d14ddbe + 4243f70 commit cdebe08

File tree

3 files changed

+51
-15
lines changed

3 files changed

+51
-15
lines changed

README.md

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -187,17 +187,19 @@ The current supported permissions are:
187187

188188
```js
189189
// example
190-
Permissions.check('location', 'always').then(response => {
190+
Permissions.check('location', { type: 'always' }).then(response => {
191191
this.setState({ locationPermission: response })
192192
})
193193

194-
Permissions.request('location', 'always').then(response => {
194+
Permissions.request('location', { type: 'always' }).then(response => {
195195
this.setState({ locationPermission: response })
196196
})
197197

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+
)
201203
```
202204

203205
* You cannot request microphone permissions on the simulator.
@@ -254,6 +256,23 @@ You can find more informations about this issue in #46.
254256
* You can request write access to any of these types by also including the
255257
appropriate write permission in the `AndroidManifest.xml` file. Read more
256258
[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+
257276
* Permissions are automatically accepted for **targetSdkVersion < 23** but you
258277
can still use `check()` to check if the user has disabled them from Settings.
259278

index.android.js

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class ReactNativePermissions {
6161
})
6262
}
6363

64-
request = permission => {
64+
request = (permission, options) => {
6565
const androidPermission = permissionTypes[permission]
6666

6767
if (!androidPermission) {
@@ -72,15 +72,22 @@ class ReactNativePermissions {
7272
)
7373
}
7474

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+
}
8179

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+
)
8491
}
8592

8693
checkMultiple = permissions =>

index.ios.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,17 @@ class ReactNativePermissions {
4242
)
4343
}
4444

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+
4656
if (!permissionTypes.includes(permission)) {
4757
return Promise.reject(
4858
`ReactNativePermissions: ${

0 commit comments

Comments
 (0)