permissions.ios.js 2.6KB

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