ReactNativePermissions.js 2.6KB

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