【问题标题】:iOS camera permissions, native iOS permission alert not firing on first requestiOS 相机权限,本机 iOS 权限警报未在第一次请求时触发
【发布时间】:2021-06-11 12:06:59
【问题描述】:

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

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

我的理解是,当第一次询问设备时,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;
        }

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

【问题讨论】:

    标签: android ios react-native permissions ios-camera


    【解决方案1】:

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

    这是适用于我的实例的解决方案。

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 嘿@Rob 感谢您的回复,这是有道理的,但即使使用response != RESULTS.GRANTED,它仍然会检查所有其他实例,如果结果“未确定”,我希望 iOS 会显示默认警报。无论哪种方式,即使作为新设备上的新用户,iOS 也没有提供该选项,当我登录 response 时,它返回被拒绝,即使我最初没有机会拒绝它。最后,即使使用自定义警报,它也会将我带到应用程序设置,但不提供相机选项。希望一切都清楚,非常感谢任何帮助。
      • 要获得系统身份验证对话框,您必须手动请求它,或者尝试仅使用相机。有关标准流程,请参阅Requesting Authorization for Media Capture on iOS。但在您这样做之前,设置甚至不会显示相机身份验证控件。而且您上面的代码没有请求任何许可,因此它在身份验证状态仍然“未确定”时遇到了if 语句,并且因为这也恰好不等于“授予”,所以您正在显示您的警报。跨度>
      • 是的,我没有意识到我并没有要求实际使用相机,哎呀!我在上面添加了解决方案
      • 感谢您指出这一点,非常感谢。感谢您的帮助/沟通。
      猜你喜欢
      • 2014-04-15
      • 2019-04-17
      • 2017-02-12
      • 1970-01-01
      • 1970-01-01
      • 2021-01-13
      • 1970-01-01
      • 2018-07-29
      • 2017-08-24
      相关资源
      最近更新 更多