ReactNativePermissions.ios.js 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. 'use strict';
  2. var React = require('react-native');
  3. var RNPermissions = React.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. ]
  16. class ReactNativePermissions {
  17. constructor() {
  18. //legacy support
  19. this.StatusUndetermined = 'undetermined'
  20. this.StatusDenied = 'denied'
  21. this.StatusAuthorized = 'authorized'
  22. this.StatusRestricted = 'restricted'
  23. RNPTypes.forEach(type => {
  24. let methodName = `${type}PermissionStatus`
  25. this[methodName] = p => {
  26. console.warn(`ReactNativePermissions: ${methodName} is depricated. Use getPermissionStatus('${type}') instead.`)
  27. return this.getPermissionStatus(p == 'reminder' ? p : type)
  28. }
  29. })
  30. }
  31. canOpenSettings() {
  32. return RNPermissions.canOpenSettings()
  33. }
  34. openSettings() {
  35. return RNPermissions.openSettings()
  36. }
  37. getPermissionTypes() {
  38. return RNPTypes;
  39. }
  40. getPermissionStatus(permission) {
  41. if (RNPTypes.includes(permission)) {
  42. return RNPermissions.getPermissionStatus(permission)
  43. } else {
  44. return Promise.reject(`ReactNativePermissions: ${permission} is not a valid permission type`)
  45. }
  46. }
  47. requestPermission(permission, type) {
  48. switch (permission) {
  49. case "location":
  50. return RNPermissions.requestLocation(type || 'always')
  51. case "camera":
  52. return RNPermissions.requestCamera();
  53. case "microphone":
  54. return RNPermissions.requestMicrophone();
  55. case "photo":
  56. return RNPermissions.requestPhoto();
  57. case "contacts":
  58. return RNPermissions.requestContacts();
  59. case "event":
  60. return RNPermissions.requestEvent();
  61. case "reminder":
  62. return RNPermissions.requestReminder();
  63. case "bluetooth":
  64. return RNPermissions.requestBluetooth();
  65. case "notification":
  66. return RNPermissions.requestNotification(type || ['alert', 'badge', 'sound'])
  67. case "backgroundRefresh":
  68. return Promise.reject('You cannot request backgroundRefresh')
  69. default:
  70. return Promise.reject('invalid type: ' + type)
  71. }
  72. }
  73. //recursive funciton to chain a promises for a list of permissions
  74. checkMultiplePermissions(permissions) {
  75. let i = permissions.length
  76. let that = this
  77. const obj = {}
  78. function processNext() {
  79. i--
  80. let p = permissions[i]
  81. if (!p) {
  82. return obj
  83. }
  84. return that.getPermissionStatus(p)
  85. .then(res => {
  86. obj[p] = res
  87. return processNext()
  88. }).catch(e => {
  89. console.warn(e)
  90. return processNext()
  91. })
  92. }
  93. return processNext()
  94. }
  95. }
  96. export default new ReactNativePermissions()