index.ios.js 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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, options) => {
  39. let type = null
  40. if (typeof options === 'string' || options instanceof Array) {
  41. console.warn(
  42. '[react-native-permissions] : You are using a deprecated version of request(). You should use an object as second parameter. Please check the documentation for more information : https://github.com/yonahforst/react-native-permissions',
  43. )
  44. type = options
  45. } else if (options != null) {
  46. type = options.type
  47. }
  48. if (!permissionTypes.includes(permission)) {
  49. return Promise.reject(
  50. `ReactNativePermissions: ${
  51. permission
  52. } is not a valid permission type on iOS`,
  53. )
  54. }
  55. if (permission == 'backgroundRefresh') {
  56. return Promise.reject(
  57. 'ReactNativePermissions: You cannot request backgroundRefresh',
  58. )
  59. }
  60. return PermissionsIOS.requestPermission(
  61. permission,
  62. type || DEFAULTS[permission],
  63. )
  64. }
  65. checkMultiple = permissions =>
  66. Promise.all(permissions.map(permission => this.check(permission))).then(
  67. result =>
  68. result.reduce((acc, value, index) => {
  69. const name = permissions[index]
  70. acc[name] = value
  71. return acc
  72. }, {}),
  73. )
  74. }
  75. module.exports = new ReactNativePermissions()