ReactNativePermissions.ios.js 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. 'use strict';
  2. var React = require('react-native');
  3. var RNPermissions = React.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. ]
  16. const permissionStatus = [
  17. 'undetermined',
  18. 'denied',
  19. 'authorized',
  20. 'restricted'
  21. ]
  22. class ReactNativePermissions {
  23. canOpenSettings() {
  24. return RNPermissions.canOpenSettings()
  25. }
  26. openSettings() {
  27. return RNPermissions.openSettings()
  28. }
  29. getPermissionStatus(permission) {
  30. if (permissionTypes.includes(permission)) {
  31. return RNPermissions.getPermissionStatus(permission)
  32. } else {
  33. return Promise.reject(`ReactNativePermissions: ${permission} is not a valid permission type`)
  34. }
  35. }
  36. requestPermission(permission, type) {
  37. switch (permission) {
  38. case 'location':
  39. return RNPermissions.requestLocation(type)
  40. case 'notification':
  41. return RNPermissions.requestNotification(type)
  42. case 'bluetooth':
  43. return RNPermissions.requestBluetooth();
  44. }
  45. }
  46. //recursive funciton to chain a promises for a list of permissions
  47. checkMultiplePermissions(permissions) {
  48. let i = permissions.length
  49. let that = this
  50. const obj = {}
  51. function processNext() {
  52. i--
  53. let p = permissions[i]
  54. if (!p) {
  55. return obj
  56. }
  57. return that.getPermissionStatus(p)
  58. .then(res => {
  59. obj[p] = res
  60. return processNext()
  61. }).catch(e => {
  62. console.warn(e)
  63. return processNext()
  64. })
  65. }
  66. return processNext()
  67. }
  68. }
  69. export default new ReactNativePermissions();