ReactNativePermissions.js 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. 'use strict';
  2. var ReactNative = require('react-native')
  3. var Platform = ReactNative.Platform
  4. var RNPermissions = ReactNative.NativeModules.ReactNativePermissions;
  5. const RNPTypes = {
  6. ios: [
  7. 'location',
  8. 'camera',
  9. 'microphone',
  10. 'photo',
  11. 'contacts',
  12. 'event',
  13. 'reminder',
  14. 'bluetooth',
  15. 'notification',
  16. 'backgroundRefresh',
  17. 'speechRecognition',
  18. ],
  19. android: [
  20. 'location',
  21. 'camera',
  22. 'microphone',
  23. 'contacts',
  24. 'event',
  25. 'photo',
  26. ]
  27. }
  28. class ReactNativePermissions {
  29. constructor() {
  30. //legacy support
  31. this.StatusUndetermined = 'undetermined'
  32. this.StatusDenied = 'denied'
  33. this.StatusAuthorized = 'authorized'
  34. this.StatusRestricted = 'restricted'
  35. this.getPermissionTypes().forEach(type => {
  36. let methodName = `${type}PermissionStatus`
  37. this[methodName] = p => {
  38. console.warn(`ReactNativePermissions: ${methodName} is depricated. Use getPermissionStatus('${type}') instead.`)
  39. return this.getPermissionStatus(p == 'reminder' ? p : type)
  40. }
  41. })
  42. }
  43. canOpenSettings() {
  44. return RNPermissions.canOpenSettings()
  45. }
  46. openSettings() {
  47. return RNPermissions.openSettings()
  48. }
  49. getPermissionTypes() {
  50. return RNPTypes[Platform.OS];
  51. }
  52. getPermissionStatus(permission) {
  53. if (this.getPermissionTypes().indexOf(permission) >= 0) {
  54. return RNPermissions.getPermissionStatus(permission)
  55. } else {
  56. return Promise.reject(`ReactNativePermissions: ${permission} is not a valid permission type on ${Platform.OS}`)
  57. }
  58. }
  59. requestPermission(permission, type) {
  60. let options;
  61. if (this.getPermissionTypes().indexOf(permission) === -1) {
  62. return Promise.reject(`ReactNativePermissions: ${permission} is not a valid permission type on ${Platform.OS}`)
  63. } else if (permission == 'backgroundRefresh'){
  64. return Promise.reject('ReactNativePermissions: You cannot request backgroundRefresh')
  65. } else if (permission == 'location') {
  66. options = type || 'whenInUse'
  67. } else if (permission == 'notification') {
  68. options = type || ['alert', 'badge', 'sound']
  69. }
  70. return RNPermissions.requestPermission(permission, options)
  71. }
  72. //recursive funciton to chain a promises for a list of permissions
  73. checkMultiplePermissions(permissions) {
  74. let i = permissions.length
  75. let that = this
  76. const obj = {}
  77. function processNext() {
  78. i--
  79. let p = permissions[i]
  80. if (!p) {
  81. return Promise.resolve(obj)
  82. }
  83. return that.getPermissionStatus(p)
  84. .then(res => {
  85. obj[p] = res
  86. return processNext()
  87. }).catch(e => {
  88. console.warn(e)
  89. return processNext()
  90. })
  91. }
  92. return processNext()
  93. }
  94. }
  95. module.exports = new ReactNativePermissions()