expo-modules
와 expo-permissions
패키지를 통해 다양한 권한 요청 기능을 제공합니다. 보통 다음과 같은 순서로 권한을 처리합니다:import * as Location from 'expo-location';
const getLocation = async () => {
const { status } = await Location.requestForegroundPermissionsAsync();
if (status !== 'granted') {
alert('위치 권한이 필요합니다.');
return;
}
const location = await Location.getCurrentPositionAsync({});
console.log(location);
};
npx expo prebuild --platform ios
npx expo run:ios
npx expo prebuild --platform android
npx expo run:android
npx expo install expo-camera
{
"expo": {
"plugins": [
[
"expo-camera",
{
"cameraPermission": "Allow $(PRODUCT_NAME) to access your camera", // 카메라 권한 요청 메시지
"microphonePermission": "Allow $(PRODUCT_NAME) to access your microphone", // 마이크 권한 요청 메시지
"recordAudioAndroid": true // 마이크 권한 요청 여부
}
]
]
}
}
import * as Camera from 'expo-camera';
const requestCameraPermission = async () => {
const { status } = await Camera.requestCameraPermissionsAsync();
if (status !== 'granted') {
alert('카메라 접근 권한이 필요합니다.');
return;
}
console.log('카메라 권한이 승인되었습니다.');
};
npx expo install expo-media-library
{
"expo": {
"plugins": [
[
"expo-media-library",
{
"photosPermission": "Allow $(PRODUCT_NAME) to access your photos.",
"savePhotosPermission": "Allow $(PRODUCT_NAME) to save photos.",
"isAccessMediaLocationEnabled": true
}
]
]
}
}
import * as MediaLibrary from 'expo-media-library';
const requestMediaLibraryPermission = async () => {
const { status } = await MediaLibrary.requestPermissionsAsync();
if (status !== 'granted') {
alert('사진 접근 권한이 필요합니다.');
return;
}
console.log('사진 권한 승인 완료');
};
npx expo install expo-notifications
import * as Notifications from 'expo-notifications';
const requestNotificationPermission = async () => {
const { status } = await Notifications.requestPermissionsAsync();
if (status !== 'granted') {
alert('알림 권한이 필요합니다.');
return;
}
console.log('알림 권한 승인됨');
};
npx expo install expo-av
{
"expo": {
"plugins": [
[
"expo-av",
{
"microphonePermission": "Allow $(PRODUCT_NAME) to access your microphone."
}
]
]
}
}
import * as Audio from 'expo-av';
const requestMicrophonePermission = async () => {
const { status } = await Audio.requestPermissionsAsync();
if (status !== 'granted') {
alert('마이크 권한이 필요합니다.');
return;
}
console.log('마이크 권한이 승인되었습니다.');
};
npx expo install expo-location
{
"expo": {
"plugins": [
[
"expo-location",
{
"locationAlwaysAndWhenInUsePermission": "Allow $(PRODUCT_NAME) to use your location."
}
]
]
}
}
import * as Location from 'expo-location';
const requestLocationPermission = async () => {
const { status } = await Location.requestForegroundPermissionsAsync();
if (status !== 'granted') {
alert('위치 권한이 필요합니다.');
return;
}
console.log('위치 권한이 승인되었습니다.');
};
undetermined
: 처음 요청하는 상태granted
: 권한 승인됨denied
: 권한 거부됨const checkCameraPermission = async () => {
const { status } = await Camera.getCameraPermissionsAsync();
if (status === 'granted') {
console.log('이미 권한이 승인됨');
} else {
console.log('권한이 아직 없거나 거부됨');
}
};