【问题标题】:React native expo iOS 无法在物理设备上请求后台位置
【发布时间】:2022-01-23 15:38:18
【问题描述】:

我正在尝试使用 expo Location 获取后台权限。这适用于 android 和 iOS 模拟器,但不适用于物理 iOS 设备。

const askLoc = async () => {
    const requestForeground = await Location.requestForegroundPermissionsAsync();
    if (requestForeground.status === 'granted') {
      const requestBackground = await Location.requestBackgroundPermissionsAsync();
      if (requestBackground.status === 'granted') {
        console.log('background location granted');
      }
    }
  };

这就是我的app.json 的样子。

"ios": {
      "infoPlist": {
        "NSCameraUsageDescription": "This app uses the camera to send pictures or create a new profile photo.",
        "NSPhotoLibraryAddUsageDescription": "User can download documents that were previously uploaded by company",
        "NSPhotoLibraryUsageDescription": "User can upload Photos/Documents to be seen by his/her company",
        "NSLocationAlwaysUsageDescription": "Location is shared with users company while he/she is completing a task/job.",
        "NSLocationWhenInUseUsageDescription": "Location is shared with users company while he/she is completing a task/job.",
        "NSUserActivityTypes": ["INSendMessageIntent", "INStartCallIntent"],
        "UIBackgroundModes": ["location", "fetch"],
        "CFBundleAllowMixedLocalizations": true
      }
    },

【问题讨论】:

    标签: ios react-native geolocation expo user-permissions


    【解决方案1】:

    调试问题需要考虑一些因素:

    1. 确保启用位置访问。 await Location.hasServicesEnabledAsync()

    2. 最近,您可能拒绝了位置许可 明确地。 requestForeground.status 将始终解析为 denied

    检测requestForeground.canAskAgain是否设置为true,否则用户已经拒绝此权限,您可以打开设备设置并手动授予用户该权限。

    这里权限响应类型定义

    export interface PermissionResponse {
      status: PermissionStatus;
      expires: PermissionExpiration;
      granted: boolean;
      canAskAgain: boolean;
    }
    

    【讨论】:

      猜你喜欢
      • 2018-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-04
      • 2021-02-19
      • 2022-06-21
      • 2020-05-17
      • 1970-01-01
      相关资源
      最近更新 更多