Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

React-native添加相机权限授权 #35

Open
ChenPt opened this issue Nov 29, 2019 · 0 comments
Open

React-native添加相机权限授权 #35

ChenPt opened this issue Nov 29, 2019 · 0 comments
Labels
RN react native

Comments

@ChenPt
Copy link
Owner

ChenPt commented Nov 29, 2019

调用相机时需要先获取相机权限,如果未校验是否拥有权限直接打开相机,app会闪退。

  1. 利用react-native的权限校验模块校验权限
    2.若有相机权限,则直接打开相机
    3.若无,则询问用户授权,确定授权后打开相机
/** Android */
showCameraScan = async (params) => {
    // android
    if (Platform.OS === 'android') {
      try {
        // 检查是否有相机权限
        const result = await PermissionsAndroid.check(PermissionsAndroid.PERMISSIONS.CAMERA)
        if(result) {
          this.cameraShow = true
          this.cameraParams = params
          return
        }
        // 请求相机权限
        const granted = await PermissionsAndroid.request(
          PermissionsAndroid.PERMISSIONS.CAMERA
        );
        if (granted === PermissionsAndroid.RESULTS.GRANTED) {
          this.cameraShow = true
          this.cameraParams = params
        } else {
          console.log('拒绝授予权限')
        }
      } catch (err) {
        console.log(err);
      }
    }
  }

该模块仅支持Android,在安卓5.0及以下,在AndroidManifest.xml文件编写

    <uses-permission android:name="android.permission.CAMERA" />
    <uses-feature android:name="android.hardware.camera" />
    <uses-feature android:name="android.hardware.camera.autofocus" />

第一次进入APP时就会自动授权。
在Android5.0以上的手机,使用相机时才会调用授权

@ChenPt ChenPt added the RN react native label Nov 29, 2019
@ChenPt ChenPt changed the title 权限 React-native添加相机权限授权 Nov 29, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
RN react native
Projects
None yet
Development

No branches or pull requests

1 participant