Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import Constants from 'expo-constants';
import * as IntentLauncher from 'expo-intent-launcher';
import * as Permissions from 'expo-permissions';
import { Alert, Linking, Platform } from 'react-native';
const PermissionName = {
[Permissions.CAMERA]: 'The Camera',
[Permissions.LOCATION]: 'GPS',
[Permissions.CAMERA_ROLL]: 'The Gallery',
[Permissions.AUDIO_RECORDING]: 'The Microphone',
[Permissions.NOTIFICATIONS]: 'Push Notifications',
[Permissions.USER_FACING_NOTIFICATIONS]: 'Notifications',
[Permissions.CONTACTS]: 'Your Contacts',
[Permissions.CALENDAR]: 'Calendar',
[Permissions.REMINDERS]: 'Reminders',
};
// Use a controlled prompt to ensure you retain access to the permission prompt.
// If the user rejects the controlled prompt, you can always prompt them again when they're ready.
// Otherwise you'll need to redirect the user to the system settings.
export async function controlledPromptAsync(permission, permissionReason, redirectReason) {
const { status } = await Permissions.getAsync(permission);
if (status === 'denied') {
return requestAsync(permission, false, permissionReason || redirectReason);
} else if (status === 'granted') {
return true;
}
_obtainUserFacingNotifPermissionsAsync = async () => {
let permission = await Permissions.getAsync(
Permissions.USER_FACING_NOTIFICATIONS
);
if (permission.status !== 'granted') {
permission = await Permissions.askAsync(
Permissions.USER_FACING_NOTIFICATIONS
);
if (permission.status !== 'granted') {
alert(`We don't have permission to present notifications.`);
}
}
return permission;
}
_obtainUserFacingNotifPermissionsAsync = async () => {
let permission = await Permissions.getAsync(
Permissions.USER_FACING_NOTIFICATIONS
);
if (permission.status !== 'granted') {
permission = await Permissions.askAsync(
Permissions.USER_FACING_NOTIFICATIONS
);
if (permission.status !== 'granted') {
alert(`We don't have permission to present notifications.`);
}
}
return permission;
}
renderSinglePermissionsButtons() {
const permissions = [
['CAMERA', Permissions.CAMERA],
['AUDIO_RECORDING', Permissions.AUDIO_RECORDING],
['LOCATION', Permissions.LOCATION],
['USER_FACING_NOTIFICATIONS', Permissions.USER_FACING_NOTIFICATIONS],
['NOTIFICATIONS', Permissions.NOTIFICATIONS],
['CONTACTS', Permissions.CONTACTS],
['SYSTEM_BRIGHTNESS', Permissions.SYSTEM_BRIGHTNESS],
['CAMERA_ROLL', Permissions.CAMERA_ROLL],
['CALENDAR', Permissions.CALENDAR],
['REMINDERS', Permissions.REMINDERS],
];
return permissions.map(([permissionName, permissionType]) => (
renderSinglePermissionsButtons() {
const permissions: Array<[string, Permissions.PermissionType]> = [
['CAMERA', Permissions.CAMERA],
['AUDIO_RECORDING', Permissions.AUDIO_RECORDING],
['LOCATION', Permissions.LOCATION],
['USER_FACING_NOTIFICATIONS', Permissions.USER_FACING_NOTIFICATIONS],
['NOTIFICATIONS', Permissions.NOTIFICATIONS],
['CONTACTS', Permissions.CONTACTS],
['SYSTEM_BRIGHTNESS', Permissions.SYSTEM_BRIGHTNESS],
['CAMERA_ROLL', Permissions.CAMERA_ROLL],
['CALENDAR', Permissions.CALENDAR],
['REMINDERS', Permissions.REMINDERS],
];
return permissions.map(([permissionName, permissionType]) => (
_obtainUserFacingNotifPermissionsAsync = async () => {
let permission = await Permissions.getAsync(
Permissions.USER_FACING_NOTIFICATIONS
);
if (permission.status !== 'granted') {
permission = await Permissions.askAsync(
Permissions.USER_FACING_NOTIFICATIONS
);
if (permission.status !== 'granted') {
Alert.alert(`We don't have permission to present notifications.`);
}
}
return permission;
}
_obtainUserFacingNotifPermissionsAsync = async () => {
let permission = await Permissions.getAsync(
Permissions.USER_FACING_NOTIFICATIONS
);
if (permission.status !== 'granted') {
permission = await Permissions.askAsync(
Permissions.USER_FACING_NOTIFICATIONS
);
if (permission.status !== 'granted') {
Alert.alert(`We don't have permission to present notifications.`);
}
}
return permission;
}
import * as Permissions from 'expo-permissions'
import React, { useEffect, useState } from 'react'
import { Button, ScrollView, Text, View } from 'react-native'
import Example from '../example'
const permissions = [
['CAMERA', Permissions.CAMERA],
['AUDIO_RECORDING', Permissions.AUDIO_RECORDING],
['LOCATION', Permissions.LOCATION],
['USER_FACING_NOTIFICATIONS', Permissions.USER_FACING_NOTIFICATIONS],
['NOTIFICATIONS', Permissions.NOTIFICATIONS],
['CONTACTS', Permissions.CONTACTS],
['SYSTEM_BRIGHTNESS', Permissions.SYSTEM_BRIGHTNESS],
['CAMERA_ROLL', Permissions.CAMERA_ROLL],
['CALENDAR', Permissions.CALENDAR],
['REMINDERS', Permissions.REMINDERS],
]
export default function PermissionsExample() {
return (
{permissions.map(([permissionName, permissionType]) => (