首页 > 解决方案 > React Native:请求权限时崩溃

问题描述

我正在按照本教程获取用户在 iOS React Native 应用程序上的位置: https ://hackernoon.com/react-native-basics-geolocation-adf3c0d10112

使用此代码获取当前位置:

navigator.geolocation.getCurrentPosition((position) => {
  console.log(position); // TBD
  this.setState({ location: true });
}, (error) => {
  console.log(error); // Handle this
  this.setState({ location: false });
}, {
  enableHighAccuracy: true,
  timeout: 20000,
  maximumAge: 1000,
});

但应用程序在此文件处崩溃:

权限Android.js:

  const shouldShowRationale = await NativeModules.PermissionsAndroid.shouldShowRequestPermissionRationale(

有错误:

TypeError:无法读取 PermissionsAndroid.Request$ 未定义的属性 shouldShowRequestPermissionRationale

但我什至没有在 Android 上运行 - 我在运行 iOS。

这可能是一个 RN 错误还是我如何使用它?

标签: iosreact-nativegeolocation

解决方案


只需要先请求权限:

await navigator.geolocation.requestAuthorization();

推荐阅读