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('[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');
  42. type = options;
  43. } else if (options != null) {
  44. type = options.type;
  45. }
  46. if (!permissionTypes.includes(permission)) {
  47. return Promise.reject(
  48. `ReactNativePermissions: ${
  49. permission
  50. } is not a valid permission type on iOS`,
  51. )
  52. }
  53. if (permission == 'backgroundRefresh') {
  54. return Promise.reject(
  55. 'ReactNativePermissions: You cannot request backgroundRefresh',
  56. )
  57. }
  58. return PermissionsIOS.requestPermission(
  59. permission,
  60. type || DEFAULTS[permission],
  61. )
  62. }
  63. checkMultiple = permissions =>
  64. Promise.all(permissions.map(permission => this.check(permission))).then(
  65. result =>
  66. result.reduce((acc, value, index) => {
  67. const name = permissions[index]
  68. acc[name] = value
  69. return acc
  70. }, {}),
  71. )
  72. }
  73. module.exports = new ReactNativePermissions()