【问题标题】:How to grant access to react-native-camera after don't allow is pressed first?在首先按下不允许后如何授予对 react-native-camera 的访问权限?
【发布时间】:2019-01-16 19:43:05
【问题描述】:

首先,当您第一次运行应用程序并转到相机时,它会在 android 或 ios 上启动 Permission 模式,并带有不允许和允许选项。

使用的包是 react-native-camera。它有一个名为 notAuthorizedView 的属性,您可以返回任何您想要的视图。我想要做的是启用相机或在不允许相机时出现的 notAuthorizedView 中授予对它的访问权限。

export default class MyCamera extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            uri:''
        };
    }

      render() {
        return (
          <View style={styles.container}>
        <RNCamera
          ref={ref => {
            this.camera = ref;
          }}
          style={styles.preview}
          type={RNCamera.Constants.Type.back}
          flashMode={RNCamera.Constants.FlashMode.on}
          notAuthorizedView={
            <View>
              <Text>YOU ARE NOT AUTHORIZED TO USE THE CAMERA</Text>
              <Button onPress={()=>{Alert.alert('SET CAMERA STATUS TO READY')}}/>
            </View>
          }
          permissionDialogTitle={'Permission to use camera'}
          permissionDialogMessage={'We need your permission to use your camera phone'}
          onGoogleVisionBarcodesDetected={({ barcodes }) => {
            console.log(barcodes);
          }}
        />
        <View style={{ flex: 0, flexDirection: 'row', justifyContent: 'center' }}>
          <TouchableOpacity onPress={this.takePicture.bind(this)} style={styles.capture}>
            <Text style={{ fontSize: 14 }}> SNAP </Text>
          </TouchableOpacity>
        </View>
      </View>
        );
      }


     goToConcern = () => {
        this.props.navigation.navigate('Concern', {uriPhoto: this.state.uri})
     };

     

  takePicture = async function() {
    if (this.camera) {
      const options = { quality: 0.5, base64: true };
      const data = await this.camera.takePictureAsync(options)
      console.log(data.uri);
      console.log(data);
      this.setState({uri:data.uri})
    }
  };
}

【问题讨论】:

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


    【解决方案1】:

    简短的回答你不能,应用程序必须做的是邀请用户更改设置并提供一个重定向到应用程序设置的按钮。

    你可以这样做:

    Linking.openURL('app-settings:')
    

    【讨论】:

    • 是的。想象一下我有应用设置,那么在这些设置中,我如何允许使用 react-native-camera 访问相机?
    • 我指的是 iOS 系统设置,而不是您的应用程序。
    • 是的,我做得对,而且效果很好!感谢您的帮助
    • 我在使用上述代码时收到此错误:- 可能未处理的承诺拒绝(id:0):错误:无法打开 URL 'app-settings:'
    • 我改用Linking.openSettings()reactnative.dev/docs/linking#opensettings
    【解决方案2】:

    由于已经设置了对 RNCamera 组件的引用,因此您只需从 RNCamera 组件实例中调用一个方法。该方法称为 refreshAuthorizationStatus() ,它返回一个 Promise,当您从 notAuthorizedView 中授予相机权限时调用此函数。希望这会有所帮助。

    render() {
        return (
           ...
           <RNCamera
              ref={ref => {
                this.camera = ref;
              }}
              ...
              notAuthButtonorizedView={
                <View>
                  <Text>YOU ARE NOT AUTHORIZED TO USE THE CAMERA</Text>
                  < onPress={async ()=>{
                     await ask_for_permission();
                     await this.camera.refreshAuthorizationStatus() <-- something like this
                  }}/>
                </View>
              }
              ...
            />
            ...
        );
    }
    

    【讨论】:

      【解决方案3】:

      android 有一个解决方法,您使用 this api ,并在您的 notAuthorizedView 按钮 onPress 中,检查相机是否被授权,如果没有,您手动请求它。但是,您需要添加一种方法来防止用户向按钮发送垃圾邮件,因为它会显示“不再询问”选项,如果用户按下,您需要将它们重定向到设置并显示 androis toast为他们提供启用相机的选项;)

      【讨论】:

        猜你喜欢
        • 2020-05-09
        • 1970-01-01
        • 1970-01-01
        • 2018-06-10
        • 1970-01-01
        • 2012-07-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多