首页 > 解决方案 > React-Native/Expo Location permissions issues

问题描述

I have a problem in relation to permissions on a React-Native Android app built using Expo. When I release an Alpha build for testing to the Play Store the app does not prompt the user to grant location permissions to the app. Currently I am trying gain permission like so:

Expo Version: "expo": "^31.0.2",

Declare my permissions in 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 />
    )
  }
}

I am not getting any of the alerts - all I am getting is my <LoadingScreen /> component spinning.

I've tried using Expo permissions module like this await Expo.Permissions.getAsync(Expo.Permissions.LOCATION); but again - the same results.

Am I missing something in relation to publishing an app to the Play Store that's built using Expo or am I trying to request permissions wrong?

标签: react-nativeexpo

解决方案


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

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

推荐阅读