Expo Permissions returns status of 'undetermined' - android

In my react native app, I ask for permission to access the camera roll as so:
getPermissionAsync = async () => {
const { status } = await Permissions.askAsync(Permissions.CAMERA_ROLL);
if (status !== "granted") {
alert("Sorry, we need camera roll permissions to make this work!");
}
await this.setState({ permission: status === "granted" });
};
_pickMedia = async (index, type) => {
if (this.state.permission != true) {
await this.getPermissionAsync();
}
if (this.state.permission == true) {
// get image
} catch (E) {
console.log(E);
}
}
};
This works as expected during testing, but for my published version on Google Play, status is returned as undetermined whether or not the user gives permission. What does this mean?

Upgrading the react-native-community#react-native-permissions to 2.0.0 solves the issue.
Refer : https://github.com/react-native-community/react-native-permissions/issues/295

Related

Error using Expo SDK 42: [Unhandled promise rejection: Error: Location provider is unavailable. Make sure that location services are enabled.] Android

I have enable location services on my Android device, but I keep getting the above error. It prompts me for my permissions request upon loading Expo (for the first time) but I still get the promise rejection. It used to work fine, but all of a sudden, stopped working. Below is my code requesting the permissions, and executing the location.
Note: This works fine on iOS, and this is in managed workflow.
useFocusEffect(
React.useCallback(()=> {
let isActive = true;
async function getLocationAsync() {
let { status } = await Location.requestForegroundPermissionsAsync()
if (status !== 'granted'){
setErrorMsg('Permission to access location was denied')
return;
}
let location = await Location.getCurrentPositionAsync({});
setLocation(location);
console.log('Location permissions granted')
}
console.log(location)
getLocationAsync()
console.log(Permissions.LOCATION)
console.log('Location status above')
return () =>{
isActive = false
}
},
[],
)
)
Call the function in the catch block until you get the coordinates.
getLocationAsync = async () => {
let { status } = await Location.requestForegroundPermissionsAsync();
if (status !== 'granted') {
this.setState({
errorMessage: 'Permission to access location was denied',
});
console.log('Permission to access location was denied')
}else{
try{
let location = await Location.getCurrentPositionAsync({ enableHighAccuracy: true });
const { latitude, longitude } = location.coords
});
}catch(error){
// Call the function until get the coordinates
this.getLocationAsync();
}
}

React Native Expo-Location returns Location service unavailable during initial useEffect

Main Problem:
I am using expo-location in my Android app in order to find the gps coordinates of the user. I have read and used the sample code provided in the expo documentation. My main problem is that, Location.getCurrentPositionAsync({}) returns Location provider is unavailable. Make sure that location services are enabled. This is my code below:
useEffect(() => {
(async () => {
let { status } = await Location.requestForegroundPermissionsAsync();
if (status !== 'granted') {
setErrorMsg('Permission to access location was denied');
return;
}
let location = await Location.getCurrentPositionAsync({});
console.log(location);
})();
}, []);
Status returns granted but getCurrentPositionAsync({}) returns an error. I implemented a janky solution by using try-catch blocks and running getCurrentPositionAsync({}) again in the catch block, which seems to work. This is my code below:
useEffect(() => {
(async () => {
let { status } = await Location.requestForegroundPermissionsAsync();
if (status !== 'granted') {
setErrorMsg('Permission to access location was denied');
return;
}
try {
var location = await Location.getCurrentPositionAsync({});
} catch {
location = await Location.getCurrentPositionAsync({});
}
console.log(location);
})();
}, []);
Does anyone know why this is happening?
EDIT: I tried to run the sample code posted in the expo-location documentation using their snack example. Same result. Could this be a problem with my phone/area? I've tested it with two phones, and both return the same error.
I think location variable can't log / use directly and this is the way that I retrieve location:
let { status } = await Location.requestPermissionsAsync();
let location = await Location.getCurrentPositionAsync({});
if (status !== 'granted') setErrorMsg('...');
const { coords } = location;
if (coords) {
const { latitude, longitude } = coords;
}

When I ask for Location permissions in Android (using React Native), permission dialog never pops up and permissions are granted automatically

I am asking for location permission for my Android React Native app. I am using the npm package: react-native-permissions. I have created a custom hook to do this.
My implementation for iOS works perfect. While trying a similar approach on Android, the dialog that asks the user for location permission never pops up. On my initial check for the permission, my app reports that permission is already granted!! But how?
I include this in my AndroidManifest.xml:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
and this is my hook that asks for permission:
export default (setPermissionsGranted = () => {}, setPermissionError = () => {}) => {
const appState = useRef(AppState.currentState)
const [shouldCheckPermission, setShoudCheckPermission] = useState(false)
useEffect(() => {
let isMounted = true
const handleAppState = (nextAppState) => {
if (appState.current.match(/inactive|background/) && nextAppState === 'active') {
// the app has come into the foreground. Change a boolean state variable to trigger
// handlePermission() again.
if (isMounted) setShoudCheckPermission(!shouldCheckPermission)
}
appState.current = nextAppState
}
const handlePermissionStatus = (result) => {
const status = {
unavailable: () => {},
denied: async () => {
const res = await request(PERMISSIONS.ANDROID.ACCESS_FINE_LOCATION)
if (isMounted) {
if (res === RESULTS.GRANTED) {
setPermissionsGranted(true)
} else {
setPermissionsGranted(false)
}
}
},
limited: () => {
// todo find out what limited entails
if (isMounted) setPermissionsGranted(true)
},
granted: async () => {
if (isMounted) setPermissionsGranted(true)
},
blocked: () => {
if (isMounted) setPermissionsGranted(false)
},
}
return status[result]
}
AppState.addEventListener('change', handleAppState)
const handlePermission = async () => {
console.log('permissions code is run')
try {
const res = await check(PERMISSIONS.ANDROID.ACCESS_FINE_LOCATION)
console.log('res', res)
handlePermissionStatus(res)()
} catch (err) {
throw err
}
}
handlePermission().catch((e) => {
if (isMounted) setPermissionError(e.message)
})
return () => {
AppState.removeEventListener('change', handleAppState)
isMounted = false
}
}, [shouldCheckPermission])
}
Any explanation as to why the user never get's asked and the permission is automatically granted?
Another peculiarity, I have commented out all of the code that requests permission for Location on Android, restarted the metro server, uninstalled the app and then re-installed it. The request-permissions tag is still in the AndroidManifest.xml. Apparently that's all I needed to do and now permissions are AUTOMATICALLY granted!
My understanding is that this is a dangerous permission and should not be granted automatically but Android is treating it as a safe-permission. 🤷🏻‍♂️
Thanks in advance.

Expo App not showing popup for Location Permission

In my expo app I want to get a user's location I am trying the same code as on the official website of Expo for getting User's location.
But expo app is not showing popup for location permission, please see the code below,
componentWillMount() {
this._getLocationAsync();
}
_getLocationAsync = async () => {
let { status } = await Permissions.askAsync(Permissions.CAMERA);
if (status !== 'granted') {
this.setState({
errorMessage: 'Permission to access location was denied',
});
}
// let location = await Location.getCurrentPositionAsync({});
// this.setState({ location });
};
On the other hand if I replace Permissions.LOCATION with Permissions.CAMERA the app shows popup for permission.
Please help I am unable to solve this issue I am using android device
First componentWillMount is deprecated. Try it with componentDidMount
Second you may already gave location permission to app. Check settings on the phone if permission is already provided. If yes, change it on the settings and try again.
componentDidMount() {
this._getLocationAsync();
}
_getLocationAsync = async () => {
let { status } = await Permissions.askAsync(Permissions.CAMERA);
if (status !== 'granted') {
this.setState({
errorMessage: 'Permission to access location was denied',
});
}
// let location = await Location.getCurrentPositionAsync({});
// this.setState({ location });
};

How to check if user has granted camera permission in React Native Android?

Right after the app launches, I am getting permission to use Camera from the user. But before opening camera if I check if the user has granted camera permission or not, I get the response as "false" even when it is allowed.
Here is my code:
PermissionsAndroid.check('camera').then(response => {
if (response === true){
//Open scanner
}
else if (response === false){
Alert("Please enable camera permission in device settings.")
}
})
Please try as following:
PermissionsAndroid.check(PermissionsAndroid.PERMISSIONS.CAMERA).then(response => { ..
and not
PermissionsAndroid.check('camera').then(response => { ..
Here I am using with async,await and promise, a little bit better to understand.
import {PermissionsAndroid} from 'react-native';
export const checkReadContactsPermission = async ()=>{
//result would be false if not granted and true if required permission is granted.
const result = await PermissionsAndroid.check(PermissionsAndroid.PERMISSIONS.CAMERA);
return result;
}
First import dependencies
import { PermissionsAndroid, Platform } from 'react-native';
Then use the following code on your event
if(Platform.OS=="android"){
const permissionAndroid = await PermissionsAndroid.check('android.permission.CAMERA');
if(permissionAndroid != PermissionsAndroid.RESULTS.granted){
const reqPer = await PermissionsAndroid.request('android.permission.CAMERA');
if(reqPer != 'granted'){
return false;
}
}
}

Categories

Resources