index.ios.js 1.6KB

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