1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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(`ReactNativePermissions: ${permission} is not a valid permission type on iOS`);
  34. }
  35. return RNPermissions.getPermissionStatus(permission, type);
  36. }
  37. request(permission, type) {
  38. if (!RNPTypes.includes(permission)) {
  39. return Promise.reject(`ReactNativePermissions: ${permission} is not a valid permission type on iOS`);
  40. }
  41. if (permission == 'backgroundRefresh') {
  42. return Promise.reject('ReactNativePermissions: You cannot request backgroundRefresh')
  43. }
  44. type = type || DEFAULTS[permission]
  45. return RNPermissions.requestPermission(permission, type)
  46. }
  47. checkMultiple(permissions) {
  48. return Promise.all(permissions.map(permission => this.check(permission)))
  49. .then(res => res.reduce((pre, cur, i) => {
  50. var name = permissions[i]
  51. pre[name] = cur
  52. return pre
  53. }, {}))
  54. }
  55. }
  56. module.exports = new ReactNativePermissions()