Request permissions properly on mobile (closes #308) (#328)

zio/stable
Paul Frazee 2023-03-20 15:16:56 -05:00 committed by GitHub
parent 4f9e9e609e
commit df6a712834
1 changed files with 10 additions and 4 deletions

View File

@ -23,7 +23,7 @@ const openPermissionAlert = (perm: string) => {
} }
export function usePhotoLibraryPermission() { export function usePhotoLibraryPermission() {
const [mediaLibraryPermissions] = MediaLibrary.usePermissions() const [res, requestPermission] = MediaLibrary.usePermissions()
const requestPhotoAccessIfNeeded = async () => { const requestPhotoAccessIfNeeded = async () => {
// On the, we use <input type="file"> to produce a filepicker // On the, we use <input type="file"> to produce a filepicker
// This does not need any permission granting. // This does not need any permission granting.
@ -31,8 +31,11 @@ export function usePhotoLibraryPermission() {
return true return true
} }
if (mediaLibraryPermissions?.status === 'granted') { if (res?.granted) {
return true return true
} else if (!res || res?.status === 'undetermined' || res?.canAskAgain) {
const updatedRes = await requestPermission()
return updatedRes?.granted
} else { } else {
openPermissionAlert('photo library') openPermissionAlert('photo library')
return false return false
@ -42,11 +45,14 @@ export function usePhotoLibraryPermission() {
} }
export function useCameraPermission() { export function useCameraPermission() {
const [cameraPermissionStatus] = Camera.useCameraPermissions() const [res, requestPermission] = Camera.useCameraPermissions()
const requestCameraAccessIfNeeded = async () => { const requestCameraAccessIfNeeded = async () => {
if (cameraPermissionStatus?.granted) { if (res?.granted) {
return true return true
} else if (!res || res?.status === 'undetermined' || res?.canAskAgain) {
const updatedRes = await requestPermission()
return updatedRes?.granted
} else { } else {
openPermissionAlert('camera') openPermissionAlert('camera')
return false return false