我正在尝试构建一个在请求许可后打开相机的应用程序。我正在使用“react-native-permissions”,它似乎找不到 RNPermissionsModule。我正在 Android 手机上测试它。目前,我有 v4.0.4 的“react-native-permissions”
这是与主要问题相关的另一个错误:
[TypeError: Cannot read property 'PERMISSIONS' of undefined]
这是我的 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;
这是我尝试过的:
const { NativeModules } = require('react-native');
if (NativeModules.RNPermissionsModule) {
console.log('RNPermissionsModule is registered!');
} else {
console.log('RNPermissionsModule not found');
}
输出:RNPermissionsModule 未找到
我在ios中也遇到了同样的错误,然后我再次阅读了react-native-permissions的文档,我开始知道pod install了。
否则请确保所有配置均已完成。