react-native - React-Native/Expo Location permissions issues
问题描述
I have a problem in relation to permissions on a React-Native Android app built using Expo. When I release an Alpha build for testing to the Play Store the app does not prompt the user to grant location permissions to the app. Currently I am trying gain permission like so:
Expo Version: "expo": "^31.0.2",
Declare my permissions in app.json
:
"permissions": [
"ACCESS_COARSE_LOCATION",
]
LandingPage.js
async componentDidMount() {
await this.requestPermission();
}
async requestPermission() {
try {
const granted = await PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.PERMISSIONS.ACCESS_COARSE_LOCATION)
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
this.watchId = navigator.geolocation.watchPosition(
position => {
const { latitude, longitude } = position.coords;
this.setState({
latitude,
longitude,
error: null,
loading: false,
});
},
error => this.setState({ error: error.message }),
{ enableHighAccuracy: true, timeout: 20000, maximumAge: 20000, distanceFilter: 10 },
);
} else {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_COARSE_LOCATION,
{
title: 'Location Access',
message:
'We need to access your location so we ' +
'can give you directions to your selecte venue.',
buttonNeutral: 'Ask Me Later',
buttonNegative: 'Cancel',
buttonPositive: 'OK',
},
);
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
alert('Location permission granted!');
this.watchId = navigator.geolocation.watchPosition(
position => {
const { latitude, longitude } = position.coords;
this.setState({
latitude,
longitude,
loading: false,
});
},
error => this.setState({ error: error.message }),
{ enableHighAccuracy: true, timeout: 20000, maximumAge: 20000, distanceFilter: 10 },
);
} else {
alert('Location permission denied');
}
}
} catch (err) {
console.warn(err)
}
}
render() {
const { loading } = this.state;
if (loading) {
return (
<LoadingScreen />
)
} else {
return (
<MainPage />
)
}
}
I am not getting any of the alerts - all I am getting is my <LoadingScreen />
component spinning.
I've tried using Expo permissions module like this await Expo.Permissions.getAsync(Expo.Permissions.LOCATION);
but again - the same results.
Am I missing something in relation to publishing an app to the Play Store that's built using Expo or am I trying to request permissions wrong?
解决方案
我不确定是什么PermissionsAndroid.RESULTS.GRANTED
。这就是我要求位置许可的方式,它有效。
let { status } = await Permissions.askAsync(Permissions.LOCATION);
if(status === 'granted') {
this.getLocation();
}
推荐阅读
- google-apps-script - 如何设置 App Script 函数以仅在其中包含“PASS”的电子邮件表行?
- python - 替换 tkinter.Text 插入方法
- gradle - Gradle 发布 jar 到 nexus repo
- javascript - 利用 !在 JSX 中的 Reactjs 组件中
- python - 在 Python 中返回哈希表中唯一的元素
- class - 使用一个类的属性,而不必每次都写下它的名字
- python - 我正在制作一个 Discord 机器人,但是当机器人使用 Rythm 命令时,什么也没有发生。有没有办法来解决这个问题?
- python - 如何在 Python Pandas 中对具有计数唯一值的多列进行分组
- python - 更新列值以反映具有两个不同大小数据框的其他列值
- swift5 - 向 CLLocationDistance 添加一些计算