首先按下不允许后如何授予对react-native-camera的访问权限?

时间:2019-01-16 19:43:05

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

首先,当您首次运行该应用并转到相机时,它会在Android或ios上使用“不允许”和“允许”选项启动“权限”模态。

使用的包装是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})
    }
  };
}

3 个答案:

答案 0 :(得分:0)

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

您可以这样做:

Linking.openURL('app-settings:')

答案 1 :(得分:0)

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

答案 2 :(得分:0)

由于已经对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>
          }
          ...
        />
        ...
    );
}