问题
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