Reputation: 343
Right after the app launches, I am getting permission to use Camera from the user. But before opening camera if I check if the user has granted camera permission or not, I get the response as "false" even when it is allowed.
Here is my code:
PermissionsAndroid.check('camera').then(response => {
if (response === true){
//Open scanner
}
else if (response === false){
Alert("Please enable camera permission in device settings.")
}
})
Upvotes: 5
Views: 26222
Reputation: 41
I tried above approaches but the return type of requesting permissions is changed to boolean value.
const permissionAndroid = await PermissionsAndroid.check('android.permission.CAMERA')
if (permissionAndroid != PermissionsAndroid.RESULTS.granted) {
...
} // this doesnt work
instead do this
const permissionAndroid = await PermissionsAndroid.check('android.permission.CAMERA')
if( permissionAndroid ){
...
}
Upvotes: 0
Reputation: 1580
First import dependencies
import { PermissionsAndroid, Platform } from 'react-native';
Then use the following code on your event
if(Platform.OS=="android"){
const permissionAndroid = await PermissionsAndroid.check('android.permission.CAMERA');
if(permissionAndroid != PermissionsAndroid.RESULTS.granted){
const reqPer = await PermissionsAndroid.request('android.permission.CAMERA');
if(reqPer != 'granted'){
return false;
}
}
}
Upvotes: 2
Reputation: 1419
Here I am using with async,await and promise, a little bit better to understand.
import {PermissionsAndroid} from 'react-native';
export const checkReadContactsPermission = async ()=>{
//result would be false if not granted and true if required permission is granted.
const result = await PermissionsAndroid.check(PermissionsAndroid.PERMISSIONS.CAMERA);
return result;
}
Upvotes: 1
Reputation: 737
Please try as following:
PermissionsAndroid.check(PermissionsAndroid.PERMISSIONS.CAMERA).then(response => { ..
and not
PermissionsAndroid.check('camera').then(response => { ..
Upvotes: 15