React Native:如何检查用户是否已授予gps权限

问题描述 投票:0回答:2

在iOS中,我正在检查应用程序是否有权在打开应用程序时访问gps。如果没有,我会显示取消或接受的提醒,这会打开设置菜单。但是,一旦用户在设置菜单中打开/关闭gps并返回应用程序,我找不到检查方法。我想等待用户从设置中返回并再次检查。

我正在使用navigator.geolocation.getCurrentPosition()Permissions.request("location")if (Permissions.canOpenSettings()){ Permissions.openSettings()...

ios reactjs geolocation gps native
2个回答
0
投票

您可以使用AppState并在应用程序再次处于活动状态时添加侦听器,以检查是否拒绝该权限。

  componentDidMount() {
    AppState.addEventListener('change', this._handleAppStateChange);
  }

  componentWillUnmount() {
    AppState.removeEventListener('change', this._handleAppStateChange);
  }

  _handleAppStateChange = (nextAppState) => {
    //Check permission here
  };

0
投票

我找到了一种解决方法,它不会在用户返回时检查它,但是在设置窗口打开时立即打开警报,所以当他回去时他必须单击确定并再次检查它:

componentDidMount() {
    this.checkPermissions();
  }

checkPermissions(){
    Permissions.check("location").then(resp => { // if resp === "denied" alert to open settings }
}

openSettings(){if (Permissions.canOpenSettings()) {
      Permissions.openSettings().then(this.renderReloadAlert());
    }}

renderReloadAlert() {
    const s = strings.initial;
    Alert.alert(
      s.alertReloadTitle,
      s.alertReloadBody,
      [
        {
          text: s.alertReloadOk,
          onPress: () => this.checkPermissions()
        }
      ],
      { cancelable: false }
    );
  }
© www.soinside.com 2019 - 2024. All rights reserved.