xRobot
xRobot

Reputation: 26567

Why PermissionAndroid doesn't work?

I get this error:

Reference Error: can't find variable PermissionsAndroid

This is my code:

AndroidManifest.xml:

<uses-permission android:name="android.permission.CAMERA" />

index.android.js:

constructor(props) {
  super(props);
  this.state = {
    message: 'Hi world'
  };
  this.requestCameraPermission();
}


async requestCameraPermission() {
  try {
    const granted = await PermissionsAndroid.requestPermission(
      PermissionsAndroid.PERMISSIONS.CAMERA,
      {
        'title': 'Cool Photo App Camera Permission',
        'message': 'Cool Photo App needs access to your camera ' +
                   'so you can take awesome pictures.'
      }
    )
    if (granted === PermissionsAndroid.RESULTS.GRANTED) {
      console.log("You can use the camera")
    } else {
      console.log("Camera permission denied")
    }
  } catch (err) {
    console.warn(err)
  }
}

Upvotes: 4

Views: 5417

Answers (1)

max23_
max23_

Reputation: 6679

Probably you forgot to import the component before using it:

import { PermissionsAndroid } from 'react-native';

Upvotes: 11

Related Questions