permissions.ios.js 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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: ${permission} 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: ${permission} is not a valid permission type on iOS`,
  54. );
  55. return Promise.reject(error);
  56. }
  57. if (permission == "backgroundRefresh") {
  58. const error = new Error(
  59. "ReactNativePermissions: You cannot request backgroundRefresh",
  60. );
  61. return Promise.reject(error);
  62. }
  63. let type;
  64. if (typeof options === "string") {
  65. type = options;
  66. } else if (options && options.type) {
  67. type = options.type;
  68. }
  69. return PermissionsIOS.requestPermission(
  70. permission,
  71. type || DEFAULTS[permission],
  72. );
  73. };
  74. checkMultiple = (permissions: Array<string>): Promise<{ [string]: string }> =>
  75. Promise.all(permissions.map(permission => this.check(permission))).then(
  76. result =>
  77. result.reduce((acc, value, index) => {
  78. const name = permissions[index];
  79. acc[name] = value;
  80. return acc;
  81. }, {}),
  82. );
  83. }
  84. export default new ReactNativePermissions();