permissions.ios.js 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // @flow
  2. import { NativeModules } from 'react-native'
  3. const PermissionsIOS = NativeModules.ReactNativePermissions
  4. type Status = 'authorized' | 'denied' | 'restricted' | 'undetermined'
  5. type Rationale = { title: string, message: string }
  6. type CheckOptions = string | { type: string }
  7. type RequestOptions = string | { type: string, rationale?: Rationale }
  8. const permissionTypes = [
  9. 'location',
  10. 'camera',
  11. 'microphone',
  12. 'photo',
  13. 'contacts',
  14. 'event',
  15. 'reminder',
  16. 'bluetooth',
  17. 'notification',
  18. 'backgroundRefresh',
  19. 'speechRecognition',
  20. 'motion',
  21. ]
  22. const DEFAULTS = {
  23. location: 'whenInUse',
  24. notification: ['alert', 'badge', 'sound'],
  25. }
  26. class ReactNativePermissions {
  27. canOpenSettings: () => Promise<boolean> = () =>
  28. PermissionsIOS.canOpenSettings()
  29. openSettings: () => Promise<*> = () => PermissionsIOS.openSettings()
  30. getTypes: () => Array<string> = () => permissionTypes
  31. check = (permission: string, options?: CheckOptions): Promise<Status> => {
  32. if (!permissionTypes.includes(permission)) {
  33. const error = new Error(
  34. `ReactNativePermissions: ${
  35. permission
  36. } is not a valid permission type on iOS`,
  37. )
  38. return Promise.reject(error)
  39. }
  40. let type
  41. if (typeof options === 'string') {
  42. type = options
  43. } else if (options && options.type) {
  44. type = options.type
  45. }
  46. return PermissionsIOS.getPermissionStatus(
  47. permission,
  48. type || DEFAULTS[permission],
  49. )
  50. }
  51. request = (permission: string, options?: RequestOptions): Promise<Status> => {
  52. if (!permissionTypes.includes(permission)) {
  53. const error = new Error(
  54. `ReactNativePermissions: ${
  55. permission
  56. } is not a valid permission type on iOS`,
  57. )
  58. return Promise.reject(error)
  59. }
  60. if (permission == 'backgroundRefresh') {
  61. const error = new Error(
  62. 'ReactNativePermissions: You cannot request backgroundRefresh',
  63. )
  64. return Promise.reject(error)
  65. }
  66. let type
  67. if (typeof options === 'string') {
  68. type = options
  69. } else if (options && options.type) {
  70. type = options.type
  71. }
  72. return PermissionsIOS.requestPermission(
  73. permission,
  74. type || DEFAULTS[permission],
  75. )
  76. }
  77. checkMultiple = (permissions: Array<string>): Promise<{ [string]: string }> =>
  78. Promise.all(permissions.map(permission => this.check(permission))).then(
  79. result =>
  80. result.reduce((acc, value, index) => {
  81. const name = permissions[index]
  82. acc[name] = value
  83. return acc
  84. }, {}),
  85. )
  86. }
  87. export default new ReactNativePermissions()