123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // @flow
  2. import { NativeModules } from 'react-native'
  3. const PermissionsIOS = NativeModules.ReactNativePermissions
  4. const permissionTypes = [
  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 = () => PermissionsIOS.canOpenSettings()
  23. openSettings = () => PermissionsIOS.openSettings()
  24. getTypes = () => permissionTypes
  25. check = (permission, type) => {
  26. if (!permissionTypes.includes(permission)) {
  27. return Promise.reject(
  28. `ReactNativePermissions: ${
  29. permission
  30. } is not a valid permission type on iOS`,
  31. )
  32. }
  33. return PermissionsIOS.getPermissionStatus(
  34. permission,
  35. type || DEFAULTS[permission],
  36. )
  37. }
  38. request = (permission, type) => {
  39. if (!permissionTypes.includes(permission)) {
  40. return Promise.reject(
  41. `ReactNativePermissions: ${
  42. permission
  43. } is not a valid permission type on iOS`,
  44. )
  45. }
  46. if (permission == 'backgroundRefresh') {
  47. return Promise.reject(
  48. 'ReactNativePermissions: You cannot request backgroundRefresh',
  49. )
  50. }
  51. return PermissionsIOS.requestPermission(
  52. permission,
  53. type || DEFAULTS[permission],
  54. )
  55. }
  56. checkMultiple = permissions =>
  57. Promise.all(permissions.map(permission => this.check(permission))).then(
  58. result =>
  59. result.reduce((acc, value, index) => {
  60. const name = permissions[index]
  61. acc[name] = value
  62. return acc
  63. }, {}),
  64. )
  65. }
  66. module.exports = new ReactNativePermissions()