index.ios.js 1.9KB

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