123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- '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()
|