permissions.ios.js 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. 'mediaLibrary',
  21. 'motion'
  22. ]
  23. const DEFAULTS = {
  24. location: 'whenInUse',
  25. notification: ['alert', 'badge', 'sound'],
  26. }
  27. class ReactNativePermissions {
  28. canOpenSettings: () => Promise<boolean> = () =>
  29. PermissionsIOS.canOpenSettings()
  30. openSettings: () => Promise<*> = () => PermissionsIOS.openSettings()
  31. getTypes: () => Array<string> = () => permissionTypes
  32. check = (permission: string, options?: CheckOptions): Promise<Status> => {
  33. if (!permissionTypes.includes(permission)) {
  34. const error = new Error(
  35. `ReactNativePermissions: ${
  36. permission
  37. } is not a valid permission type on iOS`,
  38. )
  39. return Promise.reject(error)
  40. }
  41. let type
  42. if (typeof options === 'string') {
  43. type = options
  44. } else if (options && options.type) {
  45. type = options.type
  46. }
  47. return PermissionsIOS.getPermissionStatus(
  48. permission,
  49. type || DEFAULTS[permission],
  50. )
  51. }
  52. request = (permission: string, options?: RequestOptions): Promise<Status> => {
  53. if (!permissionTypes.includes(permission)) {
  54. const error = new Error(
  55. `ReactNativePermissions: ${
  56. permission
  57. } is not a valid permission type on iOS`,
  58. )
  59. return Promise.reject(error)
  60. }
  61. if (permission == 'backgroundRefresh') {
  62. const error = new Error(
  63. 'ReactNativePermissions: You cannot request backgroundRefresh',
  64. )
  65. return Promise.reject(error)
  66. }
  67. let type
  68. if (typeof options === 'string') {
  69. type = options
  70. } else if (options && options.type) {
  71. type = options.type
  72. }
  73. return PermissionsIOS.requestPermission(
  74. permission,
  75. type || DEFAULTS[permission],
  76. )
  77. }
  78. checkMultiple = (permissions: Array<string>): Promise<{ [string]: string }> =>
  79. Promise.all(permissions.map(permission => this.check(permission))).then(
  80. result =>
  81. result.reduce((acc, value, index) => {
  82. const name = permissions[index]
  83. acc[name] = value
  84. return acc
  85. }, {}),
  86. )
  87. }
  88. export default new ReactNativePermissions()