'Trouble asking for permission with Expo Image Picker
I am trying to use Expo-image-picker with a react-native application. Here is my code:
const openCamera = async () => {
const permissionResult = await ImagePicker.requestCameraPermissionsAsync();
if (permissionResult.granted === false) {
alert("You've refused to allow this app to access your photos!");
return;
}
const result = await ImagePicker.launchCameraAsync();
if (!result.cancelled) {
uploadImage(result.uri)
}
}
Every time I try to use it it immediately jumps to the alert without prompting the user to give the camera permission. I've tried several different hooks and they all seem to not work. If anyone has a working example I'd be greatly appreciative.
Solution 1:[1]
If you've ever decline the permission, iOS remembers this and you will have to go back into Settings to allow it again. [Settings]->[Privacy]->[Photos], find ExpoGo and allow access.
Or [Settings]->[Expo Go]->[Photos]
The only way I've found to allow you to test that prompt again is to remove Expo Go and reinstall.
By the way, I think your code will immediately launch the Camera and not waiting for the permissionResult. Try this:
const openCamera = async () => {
const result = await ImagePicker.requestCameraPermissionsAsync();
if (permissionResult.granted === false) {
alert("You've refused to allow this app to access your photos!");
} else {
const result = await ImagePicker.launchCameraAsync();
if (!result.cancelled) {
uploadImage(result.uri)
}
return result;
}
}
Solution 2:[2]
The OS will only ever prompt the user once. If User denies it, then user needs to go settings. Only thing you can do is add some text or alert to show user how to allow it. ex. -> please go settings -> {AppName} -> {Permission} -> Turn On
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Can Tosun |