1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- 'use strict';
-
- const ReactNative = require('react-native');
- const RNPermissions = ReactNative.NativeModules.ReactNativePermissions;
-
- const RNPTypes = [
- 'location',
- 'camera',
- 'microphone',
- 'photo',
- 'contacts',
- 'event',
- 'reminder',
- 'bluetooth',
- 'notification',
- 'backgroundRefresh',
- 'speechRecognition',
- ]
-
- const DEFAULTS = {
- 'location' : 'whenInUse',
- 'notification': ['alert', 'badge', 'sound'],
- }
-
- class ReactNativePermissions {
- canOpenSettings() {
- return RNPermissions.canOpenSettings()
- }
-
- openSettings() {
- return RNPermissions.openSettings()
- }
-
- getTypes() {
- return RNPTypes;
- }
-
- check(permission, type) {
- if (!RNPTypes.includes(permission)) {
- return Promise.reject(`ReactNativePermissions: ${permission} is not a valid permission type on iOS`);
- }
-
- return RNPermissions.getPermissionStatus(permission, type);
- }
-
- request(permission, type) {
- if (!RNPTypes.includes(permission)) {
- return Promise.reject(`ReactNativePermissions: ${permission} is not a valid permission type on iOS`);
- }
-
- if (permission == 'backgroundRefresh') {
- return Promise.reject('ReactNativePermissions: You cannot request backgroundRefresh')
- }
-
- type = type || DEFAULTS[permission]
-
- return RNPermissions.requestPermission(permission, type)
- }
-
- checkMultiple(permissions) {
- return Promise.all(permissions.map(permission => this.check(permission)))
- .then(res => res.reduce((pre, cur, i) => {
- var name = permissions[i]
- pre[name] = cur
- return pre
- }, {}))
- }
- }
-
- module.exports = new ReactNativePermissions()
|