I'm trying to ask for Android permissions within my React Native app. I followed the official documentation here and I get this runtime error (from the react-native log-androidcommand) :
W ReactNativeJS: undefined is not an object (evaluating 'PermissionsAndroid.PERMISSIONS.CAMERA')
This is the code I actually have :
My import (generated by WebStorm) :
import * as PermissionsAndroid from "react-native";
The code that actually ask for the permission :
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.CAMERA,
{
'title': 'A title',
'message': 'A message'
}
)
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
return true;
} else {
return false;
}
} catch (err) {
console.warn(err)
}
}
I can't understand why this doesn't work since it comes from the official documentation.
In short words: you should import a single member of react-native module. This will look like this:
import { PermissionsAndroid } from 'react-native';
What causes the problem is your import syntax. In your example you are importing the whole react-native module as PermissionAndroid variable in your module. But as you see from my code line you need only a single member from the whole package.
As for me, it was really a good idea to try different ES6 import syntax in real world and to get used to them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With