index.ios.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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(permission, type)
  34. }
  35. request = (permission, type) => {
  36. if (!permissionTypes.includes(permission)) {
  37. return Promise.reject(
  38. `ReactNativePermissions: ${
  39. permission
  40. } is not a valid permission type on iOS`,
  41. )
  42. }
  43. if (permission == 'backgroundRefresh') {
  44. return Promise.reject(
  45. 'ReactNativePermissions: You cannot request backgroundRefresh',
  46. )
  47. }
  48. return PermissionsIOS.requestPermission(
  49. permission,
  50. type || DEFAULTS[permission],
  51. )
  52. }
  53. checkMultiple = permissions =>
  54. Promise.all(permissions.map(this.check)).then(result =>
  55. result.reduce((acc, value, index) => {
  56. const name = permissions[index]
  57. acc[name] = value
  58. return acc
  59. }, {}),
  60. )
  61. }
  62. export default new ReactNativePermissions()