我正在开发一个应用程序,我的用户有时需要跟踪,因为他们是在应用程序之外。我已经看过世博文档,看起来我应该使用前台和后台权限,异步函数,但是我的世博应用程序不承认这两个版本。还有其他人遇到过这个吗?我该怎么解决这个问题?下面是我的一些代码:
import * as Permissions from 'expo-permissions';
import * as Request from 'expo-permissions';
import * as Location from 'expo-location';
import Constants from 'expo-constants'
useEffect(() => {
(async () => {
if (Platform.OS === 'android' && !Constants.isDevice) {
setErrorMsg(
'Oops, this will not work on Snack in an Android emulator. Try it on your device!'
);
return;
}
let { status } = await Location.requestForegroundPermissionsAsync();
if (status !== 'granted') {
setErrorMsg('Permission to access location was denied');
return;
}
let location = await Location.getCurrentPositionAsync({});
setLocation(location);
})();
}, []);
useEffect (() =>{
(async () => {
if (Platform.OS === 'android' && !Constants.isDevice){
setErrorMsg2(
'Whoops'
);
return;
}
let { status2 } = await Location.requestBackgroundPermissionsAsync();
if (status2 !== 'granted') {
setErrorMsg2('Permission to access background location was denied');
return;
}
let location2 = await Location.getCurrentPositionAsync({})
setLocation2(location2)
})()
},[])
let text = 'Waiting..';
if (errorMsg) {
text = errorMsg;
}
else if (location) {
text = JSON.stringify(location);
}
let text2 = 'Also Wating...';
if (errorMsg2){
text2 = errorMsg2;
}
else if (location) {
text2 = JSON.stringify(location)
}我正在管理的工作流中运行博览,目前使用的是世博3.23.2版本,但我也在4.3.2版上测试过它,并且抛出了相同的错误。
发布于 2021-07-01 05:55:59
在使用expo-location时,您必须记住以下几点:
Location.requestBackgroundPermissionsAsync() 要求用户在应用程序处于后台时授予定位权限。在Android11或更高版本上:此方法将打开系统设置页面--在此之前,您应该向用户解释为什么您的应用程序需要后台位置权限。请参阅docs 这里
注意:这已经在SDK 41和expo-location**'s版本**上进行了测试。
我是如何实现它的
import React, { useState, useEffect } from 'react';
import { Platform, Text, View, StyleSheet } from 'react-native';
import * as Location from 'expo-location';
export default function App() {
const [location, setLocation] = React.useState(null);
const [errorMsg, setErrorMsg] = React.useState(null);
React.useEffect(() => {
(async () => {
let { status } = await Location.requestForegroundPermissionsAsync();
if (status !== 'granted') {
setErrorMsg('Permission to access location was denied');
return;
}
let location = await Location.getCurrentPositionAsync({});
setLocation(location);
let backPerm = await Location.requestBackgroundPermissionsAsync();
console.log(backPerm);
})();
}, []);
let text = 'Waiting..';
if (errorMsg) {
text = errorMsg;
} else if (location) {
text = JSON.stringify(location);
}
return (
<View style={styles.container}>
<Text style={styles.paragraph}>{text}</Text>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
paddingTop: 50,
backgroundColor: '#ecf0f1',
padding: 8,
},
paragraph: {
margin: 24,
fontSize: 18,
fontWeight: 'bold',
textAlign: 'center',
},
});首先,它问我前景许可,我点击了While Using the app

然后,对于背景位置权限,它会带我到设备设置页面,以允许在后台进行位置访问。

如果这不像预期的那样正常工作,请尝试以下步骤
Expo Go应用程序的所有权限。Expo go应用程序https://stackoverflow.com/questions/68203138
复制相似问题