ReactNativePermissions.js 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. 'storage',
  27. 'notification',
  28. ]
  29. }
  30. class ReactNativePermissions {
  31. constructor() {
  32. //legacy support
  33. this.StatusUndetermined = 'undetermined'
  34. this.StatusDenied = 'denied'
  35. this.StatusAuthorized = 'authorized'
  36. this.StatusRestricted = 'restricted'
  37. this.getPermissionTypes().forEach(type => {
  38. let methodName = `${type}PermissionStatus`
  39. this[methodName] = p => {
  40. console.warn(`ReactNativePermissions: ${methodName} is depricated. Use getPermissionStatus('${type}') instead.`)
  41. return this.getPermissionStatus(p == 'reminder' ? p : type)
  42. }
  43. })
  44. }
  45. canOpenSettings() {
  46. return RNPermissions.canOpenSettings()
  47. }
  48. openSettings() {
  49. return RNPermissions.openSettings()
  50. }
  51. getPermissionTypes() {
  52. return RNPTypes[Platform.OS];
  53. }
  54. getPermissionStatus(permission, type) {
  55. if (this.getPermissionTypes().indexOf(permission) >= 0) {
  56. return RNPermissions.getPermissionStatus(permission, type)
  57. } else {
  58. return Promise.reject(`ReactNativePermissions: ${permission} is not a valid permission type on ${Platform.OS}`)
  59. }
  60. }
  61. requestPermission(permission, type) {
  62. let options;
  63. if (this.getPermissionTypes().indexOf(permission) === -1) {
  64. return Promise.reject(`ReactNativePermissions: ${permission} is not a valid permission type on ${Platform.OS}`)
  65. } else if (permission == 'backgroundRefresh'){
  66. return Promise.reject('ReactNativePermissions: You cannot request backgroundRefresh')
  67. } else if (permission == 'location') {
  68. options = type || 'whenInUse'
  69. } else if (permission == 'notification') {
  70. if (Platform.OS === 'android') {
  71. return Promise.reject(`ReactNativePermissions: notification cannot be requested on Android`)
  72. }
  73. options = type || ['alert', 'badge', 'sound']
  74. }
  75. return RNPermissions.requestPermission(permission, options)
  76. }
  77. //recursive funciton to chain a promises for a list of permissions
  78. checkMultiplePermissions(permissions) {
  79. let i = permissions.length
  80. let that = this
  81. const obj = {}
  82. function processNext() {
  83. i--
  84. let p = permissions[i]
  85. if (!p) {
  86. return Promise.resolve(obj)
  87. }
  88. return that.getPermissionStatus(p)
  89. .then(res => {
  90. obj[p] = res
  91. return processNext()
  92. }).catch(e => {
  93. console.warn(e)
  94. return processNext()
  95. })
  96. }
  97. return processNext()
  98. }
  99. }
  100. module.exports = new ReactNativePermissions()