浏览代码

Merge pull request #192 from axsann/master

Add support for permission request rationale. (resolve conflicts)
Mathieu Acthernoene 8 年之前
父节点
当前提交
4243f704e2
共有 3 个文件被更改,包括 51 次插入15 次删除
  1. 24 5
      README.md
  2. 16 9
      index.android.js
  3. 11 1
      index.ios.js

+ 24 - 5
README.md

@@ -187,17 +187,19 @@ The current supported permissions are:
 
 ```js
 // example
-Permissions.check('location', 'always').then(response => {
+Permissions.check('location', { type: 'always' }).then(response => {
   this.setState({ locationPermission: response })
 })
 
-Permissions.request('location', 'always').then(response => {
+Permissions.request('location', { type: 'always' }).then(response => {
   this.setState({ locationPermission: response })
 })
 
-Permissions.request('notification', ['alert', 'badge']).then(response => {
-  this.setState({ notificationPermission: response })
-})
+Permissions.request('notification', { type: ['alert', 'badge'] }).then(
+  response => {
+    this.setState({ notificationPermission: response })
+  },
+)
 ```
 
 * You cannot request microphone permissions on the simulator.
@@ -254,6 +256,23 @@ You can find more informations about this issue in #46.
 * 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).
+
+* The optional rationale argument will show a dialog prompt.
+
+```js
+// example
+Permissions.request('camera', {
+  rationale: {
+    title: 'Cool Photo App Camera Permission',
+    message:
+      'Cool Photo App needs access to your camera ' +
+      'so you can take awesome pictures.',
+  },
+}).then(response => {
+  this.setState({ cameraPermission: response })
+})
+```
+
 * Permissions are automatically accepted for **targetSdkVersion < 23** but you
   can still use `check()` to check if the user has disabled them from Settings.
 

+ 16 - 9
index.android.js

@@ -61,7 +61,7 @@ class ReactNativePermissions {
     })
   }
 
-  request = permission => {
+  request = (permission, options) => {
     const androidPermission = permissionTypes[permission]
 
     if (!androidPermission) {
@@ -72,15 +72,22 @@ class ReactNativePermissions {
       )
     }
 
-    return PermissionsAndroid.request(androidPermission).then(result => {
-      // PermissionsAndroid.request() to native module resolves to boolean
-      // rather than string if running on OS version prior to Android M
-      if (typeof result === 'boolean') {
-        return result ? 'authorized' : 'denied'
-      }
+    let rationale = null
+    if (options != null) {
+      rationale = options.rationale
+    }
 
-      return setDidAskOnce(permission).then(() => RESULTS[result])
-    })
+    return PermissionsAndroid.request(androidPermission, rationale).then(
+      result => {
+        // PermissionsAndroid.request() to native module resolves to boolean
+        // rather than string if running on OS version prior to Android M
+        if (typeof result === 'boolean') {
+          return result ? 'authorized' : 'denied'
+        }
+
+        return setDidAskOnce(permission).then(() => RESULTS[result])
+      },
+    )
   }
 
   checkMultiple = permissions =>

+ 11 - 1
index.ios.js

@@ -42,7 +42,17 @@ class ReactNativePermissions {
     )
   }
 
-  request = (permission, type) => {
+  request = (permission, options) => {
+    let type = null
+    if (typeof options === 'string' || options instanceof Array) {
+      console.warn(
+        '[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',
+      )
+      type = options
+    } else if (options != null) {
+      type = options.type
+    }
+
     if (!permissionTypes.includes(permission)) {
       return Promise.reject(
         `ReactNativePermissions: ${