I am trying to build an application that opens a camera after asking for permission. I am using 'react-native-permissions' and it seems that it cannot find the RNPermissionsModule. I am testing it on an android phone. Currently, I have v4.0.4 of 'react-native-permissions'
Here is another error that's connected with the main issue:
[TypeError: Cannot read property 'PERMISSIONS' of undefined]
Here is my Permissions.js
import { Alert, Platform } from 'react-native';
import { check, PERMISSIONS, RESULTS, request, openSettings } from 'react-native-permissions';
export const isIOS = Platform.OS === 'ios';
function showAlert(msg) {
Alert.alert('', msg, [
{
text: 'Cancel',
onPress: () => console.log('Cancel Pressed'),
style: 'cancel',
},
{
text: 'Settings',
onPress: () => {
openSettings().catch(() => console.warn('cannot open settings'));
},
},
]);
}
const hasCameraPermission = async (withAlert = true) => {
try {
const permission = isIOS
? PERMISSIONS.IOS.CAMERA
: PERMISSIONS.ANDROID.CAMERA;
const response = await check(permission);
let camera;
if (response.camera !== RESULTS.GRANTED) {
camera = await request(permission);
}
if (camera === RESULTS.DENIED || camera === RESULTS.BLOCKED) {
if (withAlert) {
showAlert(
'Permission not granted for Camera. Unable to use camera in this application.',
);
}
return false;
}
return true;
} catch (error) {
console.log(error);
return false;
}
};
const hasPhotoPermission = async (withAlert = true) => {
try {
const permission = isIOS
? PERMISSIONS.IOS.PHOTO_LIBRARY
: PERMISSIONS.ANDROID.WRITE_EXTERNAL_STORAGE_StorageService;
const response = await check(permission);
let photo;
if (response.photo !== RESULTS.GRANTED) {
photo = await request(permission);
}
if (photo === RESULTS.DENIED || photo === RESULTS.BLOCKED) {
if (withAlert) {
showAlert(
'Permission not granted for photos. Unable to get photos in this application.',
);
}
return false;
}
return true;
} catch (error) {
console.log(error);
return false;
}
};
const PermissionsService = {
hasCameraPermission,
hasPhotoPermission,
}
export default PermissionsService;
Here's what I have tried:
- Reinstalling 'react-native-permissions'
- Downgrading 'react-native-permissions'
- cleaning the cache
- Check if RNPermissionsModule is registered using
const { NativeModules } = require('react-native');
if (NativeModules.RNPermissionsModule) {
console.log('RNPermissionsModule is registered!');
} else {
console.log('RNPermissionsModule not found');
}
Output: RNPermissionsModule not found