iOS 相机权限,本地 iOS 权限警报不会在第一次请求时触发

问题描述

大家好,我有一个应用程序,它允许用户在执行任务时拍照。

最近在第一次请求相机权限时,设备没有显示本机警报,而是推迟到我的辅助警报,如果用户在第一次尝试后拒绝或更改了他们的权限设置,则应该使用该警报。

我的理解是,当第一次询问设备时,iOS 会提供类似这样的权限提示

enter image description here

我的 info.plist

中有这个
    <key>NSCameraUsageDescription</key>
    <string>Allow access to your camera to take pictures of your dog!</string>

用户在应用中点击相机按钮时的代码底部解决方案)

  function takePhoto() {
    check(
      Platform.select({
        ios: PERMISSIONS.IOS.CAMERA,android: PERMISSIONS.ANDROID.CAMERA,})
    )
      .then(async (response) => {
        if (response == 'unavailable') {
          alert('Camera is not available on this device');
          return;
        }

        const userId = auth().currentUser.uid;

        let image = null;
        const mapAPI = new MapAPI(firestore(),userId,storage);

        const showSettingsAlert = (title,message) => {
          Alert.alert(
            title,message,[
              {
                text: translations['settings.goto'],onPress: async () => {
                  Linking.openSettings();
                },},{
                text: translations['cancel'],onPress: () => {
                  console.log('Cancel pressed');
                },style: 'cancel',],{ cancelable: true }
          );
        };

        if (response != RESULTS.GRANTED) {
          showSettingsAlert(
            translations['permissions.cameratitle'],translations['permissions.cameradesc']
          );
          return;
        }

我一直在努力解决这个问题,感谢您的帮助。谢谢!

解决方法

您正在测试 response != RESULTS.GRANTED。因此,如果“未确定”(询问用户权限之前的状态),则会导致您发出警报。如果状态为“拒绝”或“受限”,而不是“授予”,我们通常会显示我们的自定义提醒。

,

在另一个溢出者的帮助下,我意识到在调用自定义警报之前我没有请求访问相机。

这是在我的实例中有效的解决方案。

  const takePhoto = async () => {
    const imageProps = {
      mediaType: 'photo',width: 400,height: 400,writeTempFile: true,};

    const userId = auth().currentUser.uid;

    let image = null;
    const mapAPI = new MapAPI(firestore(),userId,storage);

    try {
      image = await ImageCropPicker.openCamera(imageProps);
    } catch (error) {
      console.log(error);

      const response = await check(
        Platform.select({
          ios: PERMISSIONS.IOS.CAMERA,android: PERMISSIONS.ANDROID.CAMERA,})
      );
      if (response !== RESULTS.GRANTED && response !== RESULTS.UNAVAILABLE) {
        showSettingsAlert(
          translations['permissions.cameratitle'],translations['permissions.cameradesc']
        );
      }
    }

    if (!image) {
      return;
    }