ReactNativePermissions.js 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. options = type || ['alert', 'badge', 'sound']
  71. }
  72. return RNPermissions.requestPermission(permission, options)
  73. }
  74. //recursive funciton to chain a promises for a list of permissions
  75. checkMultiplePermissions(permissions) {
  76. let i = permissions.length
  77. let that = this
  78. const obj = {}
  79. function processNext() {
  80. i--
  81. let p = permissions[i]
  82. if (!p) {
  83. return Promise.resolve(obj)
  84. }
  85. return that.getPermissionStatus(p)
  86. .then(res => {
  87. obj[p] = res
  88. return processNext()
  89. }).catch(e => {
  90. console.warn(e)
  91. return processNext()
  92. })
  93. }
  94. return processNext()
  95. }
  96. }
  97. module.exports = new ReactNativePermissions()