【问题标题】:Expo Location Error: Not authorized to use background location servicesExpo Location Error: 无权使用后台定位服务
【发布时间】:2021-04-04 17:12:39
【问题描述】:

我正在创建一个需要用户当前位置并且需要实时运行的应用。我正在使用来自世博会的 Location.startLocationUpdatesAsync() 但我收到以下错误。 app.json 已经配置,并且在手机中我接受了位置权限。有人可以告诉我为什么会出现错误?

app.json


    "ios": {
      "supportsTablet": true,
      "bundleIdentifier": "com.ourtor.ourtorapp",
      "buildNumber": "0.0.8",
     "infoPlist": { "UIBackgroundModes": [ "location", "fetch" ] }
    },
    "android": {
      "adaptiveIcon": {
        "foregroundImage": "./assets/adaptive-icon.png",
        "backgroundColor": "#FFFFFF"
      },
      "permissions": [
        "ACCESS_COARSE_LOCATION",
        "ACCESS_FINE_LOCATION",
        "ACCESS_BACKGROUND_LOCATION"
      ]
    },

map.ts

  useEffect( () => {

    Location.requestPermissionsAsync().then( async (res) => {
      if(res.status === "granted") {
        console.log('granted')
        await Location.startLocationUpdatesAsync('get-location', {
          accuracy: Location.Accuracy.Balanced
        })
      }
      
    });
    
  }, [])

控制台安卓

granted
[Unhandled promise rejection: Error: Not authorized to use background location services.]

控制台 ios

granted
Background Location has not been configured. To enable it, add 'location' to 'UIBackgroundModes' in Info.plist file.

【问题讨论】:

    标签: typescript react-native location expo


    【解决方案1】:

    你真的需要背景位置吗?似乎为 background 使用配置 Location 需要做更多的工作。 Location 的配置矩阵很复杂(Android、Apple、Emulators、Permissions、Foreground\Background,甚至是 Android 的应用提交请求)。我会确保您已经彻底阅读了 Expo 的文档,因为所有这些细节都分散在整个页面中:https://docs.expo.io/versions/latest/sdk/location/#background-location-methods

    也就是说,当应用在 Android 上的 前台 中时,我在获取位置时遇到了类似的问题。我必须确保在 Android 应用设置中为 Expo Go 开启了 Location。但这还不够,需要调用 Location.getPermissionAsync() (它不在我的 Apple 设备上)。然后我就可以毫无问题地使用 Location.watchPositionAsync(...)。

    我要提到的另一件事是,当 Expo Go 中的 Location 存在较大问题时(当时它仍然是 Expo Client),您的问题就会出现。您可以查看此 GitHub 问题以获取更多详细信息:https://github.com/expo/expo/issues/11330。 TLDR;如果您在 Expo Go > 2.18.5 上,您应该不会再看到这个问题了。

    【讨论】:

      【解决方案2】:

      使用 expo 权限有更好的方法来做到这一点 更多信息请查看https://docs.expo.io/versions/latest/sdk/permissions/

      【讨论】:

      • 这个方法我已经试过了,不行
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-27
      • 2021-06-11
      相关资源
      最近更新 更多