React native: crash while requesting permissions

走远了吗. 提交于 2019-12-13 06:34:21

问题


I'm following this tutorial to get user's location on iOS React Native app: https://hackernoon.com/react-native-basics-geolocation-adf3c0d10112

Using this code to get current location:

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,
});

But the app crashes at this file:

PermissionsAndroid.js:

  const shouldShowRationale = await NativeModules.PermissionsAndroid.shouldShowRequestPermissionRationale(

with error:

TypeError: Cannot read property shouldShowRequestPermissionRationale of undefined at PermissionsAndroid.Request$

But I'm not even running on Android - I'm running iOS.

Could this be a RN bug or how I'm using it?


回答1:


Just had to request permissions first:

await navigator.geolocation.requestAuthorization();


来源:https://stackoverflow.com/questions/53605192/react-native-crash-while-requesting-permissions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!