React Native:请求权限时崩溃 [英] React native: crash while requesting permissions

查看:197
本文介绍了React Native:请求权限时崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

PermissionsAndroid.js:

  const shouldShowRationale = await NativeModules.PermissionsAndroid.shouldShowRequestPermissionRationale(

有错误:

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

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

但是我什至没有在Android上运行-我正在运行iOS.

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

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

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

推荐答案

只需要首先请求权限:

await navigator.geolocation.requestAuthorization();

这篇关于React Native:请求权限时崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆