iOS 相机权限,原生 iOS 权限警报未在第一次请求时触发

iOS camera permissions, native iOS permission alert not firing on first request

大家好我有一个应用程序允许用户在任务期间拍照。

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

我的理解是,当设备第一次被请求时 iOS 将提供类似于此的权限警报

我的 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;
    }