我正在构建一个需要后台位置获取的 Expo 应用程序。它在 Android 上正常工作,但在 iOS 上,我收到错误消息“错误:Info.plist 中必须存在 NSLocation*UsageDescription 键之一才能使用地理定位。”当使用 Expo Go 在 iOS 上运行应用程序时,我没有“始终”启用位置的选项。
世博 SDK 48。
这是我的 app.json 文件:
{
"expo": {
"name": "***",
"slug": "***",
"version": "1.0.0",
"orientation": "portrait",
"icon": "***",
"userInterfaceStyle": "light",
"splash": {
"image": "./assets/logo-transparent.png",
"backgroundColor": "#566ff9"
},
"assetBundlePatterns": [
"**/*"
],
"ios": {
"supportsTablet": true,
"splash": {
"image": "./assets/logo-transparent.png",
"backgroundColor": "#566ff9"
},
"bundleIdentifier": "***",
"googleServicesFile": "./GoogleService-Info.plist",
"infoPlist": {
"NSLocationWhenInUseUsageDescription": "This app needs access to your location to show you nearby places.",
"NSLocationAlwaysUsageDescription": "This app needs access to your location to show you nearby places.",
"NSLocationUsageDescription": "This app needs access to your location to show you nearby places.",
"NSLocationAlwaysAndWhenInUseUsageDescription": "This app needs access to your location to show you nearby places.",
"UIBackgroundModes": [
"location",
"fetch"
]
}
},
"android": {
"adaptiveIcon": {
"foregroundImage": "***",
"backgroundColor": "#566ff9"
},
"package": "***",
"googleServicesFile": "./google-services.json",
"permissions": [
"ACCESS_FINE_LOCATION",
"ACCESS_COARSE_LOCATION",
"READ_EXTERNAL_STORAGE",
"WRITE_EXTERNAL_STORAGE",
"ACCESS_NETWORK_STATE",
"ACCESS_WIFI_STATE",
"INTERNET",
"LOCATION",
"NOTIFICATIONS",
"BACKGROUND_FETCH",
"FOREGROUND_SERVICE",
"ACCESS_BACKGROUND_LOCATION"
]
},
"plugins": [
"expo-localization",
[
"expo-location",
{
"locationAlwaysAndWhenInUsePermission": "Allow *** to use your location.",
"locationAlwaysPermission": "Allow *** to use your location.",
"locationWhenInUsePermission": "Allow *** to use your location."
}
]
]
}
}
这是我的 package.json 文件:
{
"name": "***",
"version": "1.0.0",
"main": "node_modules/expo/AppEntry.js",
"scripts": {
"start": "expo start",
"android": "expo start --android",
"ios": "expo start --ios",
"web": "expo start --web"
},
"dependencies": {
"@expo/config-plugins": "~6.0.0",
"@react-native-async-storage/async-storage": "1.17.11",
"@react-native-community/datetimepicker": "6.7.3",
"@react-native-firebase/app": "^17.4.2",
"@react-native-firebase/database": "^17.4.2",
"@react-navigation/bottom-tabs": "^6.5.7",
"@react-navigation/drawer": "^6.6.2",
"@react-navigation/native": "^6.1.6",
"@react-navigation/native-stack": "^6.9.12",
"@react-navigation/stack": "^6.3.16",
"@rneui/themed": "^4.0.0-rc.7",
"axios": "^1.3.4",
"expo": "^48.0.0",
"expo-background-fetch": "~11.1.1",
"expo-image-multiple-picker": "^4.8.3",
"expo-image-picker": "~14.1.1",
"expo-keep-awake": "~12.0.1",
"expo-linear-gradient": "~12.1.2",
"expo-localization": "~14.1.1",
"expo-location": "~15.1.1",
"expo-media-library": "~15.2.3",
"expo-notifications": "~0.18.1",
"expo-radio-button": "^1.0.8",
"expo-status-bar": "~1.4.4",
"expo-task-manager": "~11.1.1",
"firebase": "^9.18.0",
"i18n-js": "^4.2.3",
"lottie-react-native": "5.1.4",
"radio-buttons-react-native-expo": "^1.1.0",
"react": "18.2.0",
"react-native": "0.71.8",
"react-native-action-button": "^2.8.5",
"react-native-actionsheet": "^2.4.2",
"react-native-calendars": "^1.1294.0",
"react-native-dropdown-select-list": "^2.0.4",
"react-native-gesture-handler": "~2.9.0",
"react-native-image-zoom-viewer": "^3.0.1",
"react-native-keyboard-aware-scroll-view": "^0.9.5",
"react-native-linear-gradient": "^2.6.2",
"react-native-modern-datepicker": "^1.0.0-beta.91",
"react-native-paper": "^3.12.0",
"react-native-radio-buttons-group": "^2.3.2",
"react-native-reanimated": "^2.14.4",
"react-native-round-flags": "^1.0.4",
"react-native-safe-area-context": "4.5.0",
"react-native-screens": "~3.20.0",
"react-native-svg": "^13.4.0",
"react-native-swipe-list-view": "^3.2.9",
"react-native-vector-icons": "^9.2.0",
"react-navigation": "^4.4.4",
"react-navigation-material-bottom-tabs": "^2.3.5",
"save": "^2.9.0"
},
"devDependencies": {
"@babel/core": "^7.20.0"
},
"resolutions": {
"@expo/config-plugins": "~6.0.0",
"@expo/prebuild-config": "~6.0.0"
},
"private": true
}
我使用以下代码来请求位置权限:
useEffect(() => {
const requestPermissions = async () => {
const foreground = await Location.requestForegroundPermissionsAsync()
if (foreground.granted) await Location.requestBackgroundPermissionsAsync()
}
requestPermissions()
}, [])
对于那些仍在苦苦挣扎的人,我找到了一个解决方案,我已将其发布在这里:错误:Info.plist 中必须存在“NSLocation*UsageDescription”键之一才能使用地理定位