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

How to grant access to react-native-camera after don't allow is pressed first?

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

使用的包是react-native-camera。它有一个名为 notAuthorizedView 的 属性,您可以 return 任何您想要的视图。我想要做的是在不允许使用相机时出现的 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})
    }
  };
}

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

你可以这样做:

Linking.openURL('app-settings:')

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

由于已经有RNCamera 组件的引用集,您只需调用RNCamera 组件实例的方法即可。该方法称为 refreshAuthorizationStatus(),其中 returns 一个 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>
          }
          ...
        />
        ...
    );
}