ReactNativePermissions.js 2.5KB

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