123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. 'use strict'
  2. const ReactNative = require('react-native')
  3. const RNPermissions = ReactNative.NativeModules.ReactNativePermissions
  4. const RNPTypes = [
  5. 'location',
  6. 'camera',
  7. 'microphone',
  8. 'photo',
  9. 'contacts',
  10. 'event',
  11. 'reminder',
  12. 'bluetooth',
  13. 'notification',
  14. 'backgroundRefresh',
  15. 'speechRecognition',
  16. ]
  17. const DEFAULTS = {
  18. location: 'whenInUse',
  19. notification: ['alert', 'badge', 'sound'],
  20. }
  21. class ReactNativePermissions {
  22. canOpenSettings() {
  23. return RNPermissions.canOpenSettings()
  24. }
  25. openSettings() {
  26. return RNPermissions.openSettings()
  27. }
  28. getTypes() {
  29. return RNPTypes
  30. }
  31. check(permission, type) {
  32. if (!RNPTypes.includes(permission)) {
  33. return Promise.reject(
  34. `ReactNativePermissions: ${
  35. permission
  36. } is not a valid permission type on iOS`,
  37. )
  38. }
  39. return RNPermissions.getPermissionStatus(permission, type)
  40. }
  41. request(permission, type) {
  42. if (!RNPTypes.includes(permission)) {
  43. return Promise.reject(
  44. `ReactNativePermissions: ${
  45. permission
  46. } is not a valid permission type on iOS`,
  47. )
  48. }
  49. if (permission == 'backgroundRefresh') {
  50. return Promise.reject(
  51. 'ReactNativePermissions: You cannot request backgroundRefresh',
  52. )
  53. }
  54. type = type || DEFAULTS[permission]
  55. return RNPermissions.requestPermission(permission, type)
  56. }
  57. checkMultiple(permissions) {
  58. return Promise.all(
  59. permissions.map(permission => this.check(permission)),
  60. ).then(res =>
  61. res.reduce((pre, cur, i) => {
  62. var name = permissions[i]
  63. pre[name] = cur
  64. return pre
  65. }, {}),
  66. )
  67. }
  68. }
  69. module.exports = new ReactNativePermissions()