"Location Permission Denied" 在 ios 模拟器上,即使在 Features->Location->customlocation 中设置了自定义位置之后

"Location Permission Denied" on ios simulator even after setting custom location in Features->Location->customlocation

我是移动开发新手。我正在尝试在 ios 模拟器 运行 IOS 14.4 中访问我的当前位置,即使在设置了 自定义位置后我仍然收到“位置权限被拒绝”错误在模拟器 中,正如该平台上一些答案所建议的那样,。我是运行的代码是这个

Geolocation.getCurrentPosition(
    position => {
      this.setState({
        latitude: position.coords.latitude,
        longitude: position.coords.longitude,
        coordinates: this.state.coordinates.concat({
          latitude: position.coords.latitude,
          longitude: position.coords.longitude
        })
      });
    },
    error => {
      Alert.alert(error.message.toString());
    },
    {
      showLocationDialog: true,
      enableHighAccuracy: true,
      timeout: 20000,
      maximumAge: 0
    }
  );

我已经在我的 info-plist 中设置了这些权限。

<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>Description</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>Will you allow this app to always know your location?</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>Do you allow this app to know your current location?</string>

我不完全确定我在这里做错了什么。这是目前唯一尝试访问该位置的代码。

是否有任何其他原因导致即使在功能->位置->自定义位置中将当前位置设置为自定义位置后此错误仍然存​​在?正如该平台上其他答案所建议的那样。

在您 运行 需要位置访问权限的代码之前,需要在 run-time 处请求位置访问权限。

react-native-permissions 是 cross-platform 权限的标准库

按照此处的安装说明进行操作 - https://www.npmjs.com/package/react-native-permissions

import { request, PERMISSIONS, RESULT } from "react-native-permissions";

function getUserLocation() {
  Geolocation.getCurrentPosition(
    (position) => {
      this.setState({
        latitude: position.coords.latitude,
        longitude: position.coords.longitude,
        coordinates: this.state.coordinates.concat({
          latitude: position.coords.latitude,
          longitude: position.coords.longitude,
        }),
      });
    },
    (error) => {
      Alert.alert(error.message.toString());
    },
    {
      showLocationDialog: true,
      enableHighAccuracy: true,
      timeout: 20000,
      maximumAge: 0,
    }
  );
}
request(PERMISSIONS.IOS.LOCATION_ALWAYS).then((result) => {
  switch (result) {
    case RESULTS.UNAVAILABLE:
      console.log(
        "This feature is not available (on this device / in this context)"
      );
      break;
    case RESULTS.DENIED:
      console.log(
        "The permission has not been requested / is denied but requestable"
      );
      break;
    case RESULTS.LIMITED:
      console.log("The permission is limited: some actions are possible");
      break;
    case RESULTS.GRANTED:
      console.log("The permission is granted");
      // Permission has been granted - app can request location coordinates
      getUserLocation();
      break;
    case RESULTS.BLOCKED:
      console.log("The permission is denied and not requestable anymore");
      break;
  }
});