React-Native / Expo Location权限问题

时间:2019-03-04 21:08:42

标签: react-native expo

我对使用Expo构建的React-Native Android应用程序的权限存在问题。当我向Play商店发布用于测试的Alpha版本时,该应用不会提示用户向该应用授予位置权限。目前,我正在尝试像这样获得许可:

展览会版本:"expo": "^31.0.2",

app.json中声明我的权限:

"permissions": [
        "ACCESS_COARSE_LOCATION",
      ]

LandingPage.js

async componentDidMount() {
  await this.requestPermission();
}


async requestPermission() {
    try {
      const granted = await PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.PERMISSIONS.ACCESS_COARSE_LOCATION)

      if (granted === PermissionsAndroid.RESULTS.GRANTED) {
        this.watchId = navigator.geolocation.watchPosition(
          position => {
            const { latitude, longitude } = position.coords;

            this.setState({
              latitude,
              longitude,
              error: null,
              loading: false,

            });
          },
          error => this.setState({ error: error.message }),
          { enableHighAccuracy: true, timeout: 20000, maximumAge: 20000, distanceFilter: 10 },
        );
      } else {
        const granted = await PermissionsAndroid.request(
          PermissionsAndroid.PERMISSIONS.ACCESS_COARSE_LOCATION,
          {
            title: 'Location Access',
            message:
              'We need to access your location so we ' +
              'can give you directions to your selecte venue.',
            buttonNeutral: 'Ask Me Later',
            buttonNegative: 'Cancel',
            buttonPositive: 'OK',
          },
        );
        if (granted === PermissionsAndroid.RESULTS.GRANTED) {
          alert('Location permission granted!');
          this.watchId = navigator.geolocation.watchPosition(
            position => {
              const { latitude, longitude } = position.coords;

              this.setState({
                latitude,
                longitude,
                loading: false,
              });

            },
            error => this.setState({ error: error.message }),
            { enableHighAccuracy: true, timeout: 20000, maximumAge: 20000, distanceFilter: 10 },
          );
        } else {
          alert('Location permission denied');
        }
      }
    } catch (err) {
      console.warn(err)
    }
  }


render() {
  const { loading } = this.state;

  if (loading) {
    return (
      <LoadingScreen />
    )
  } else {
    return (
      <MainPage />
    )
  }
}

我没有收到任何警报-我得到的只是<LoadingScreen />组件旋转。

我已经尝试过使用await Expo.Permissions.getAsync(Expo.Permissions.LOCATION);这样的Expo权限模块,但是再次使用-相同的结果。

我是否缺少与将应用程序发布到使用Expo构建的Play商店相关的内容,还是我尝试请求错误的权限?

1 个答案:

答案 0 :(得分:0)

我不确定PermissionsAndroid.RESULTS.GRANTED是什么。这就是我请求位置许可的方式,并且有效。

let { status } = await Permissions.askAsync(Permissions.LOCATION);
        if(status === 'granted') {
            this.getLocation();
         }