为什么权限位置警报没有出现?

时间:2019-10-03 09:28:44

标签: android react-native geolocation android-permissions

我正在尝试在我的本机android应用上显示位置权限警报,但没有出现。

这是我的代码,首先是用来检查权限的函数:

checkPermission = async cb => {
    if (Platform.OS === "android") {
      try {
        const granted = await PermissionsAndroid.request(
          PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
          {
            title: "IZI CHECK",
            message: translate("access_location_msg")
          }
        );
        console.log("granted:", granted);
        if (granted === PermissionsAndroid.RESULTS.GRANTED) {
          cb();
        } else {
          alert(translate("location_permission_denied"));
        }
        console.log("Granted:", granted);
      } catch (err) {
        console.warn(err);
      }
    } else {
      cb();
    }
  };

最后,我致电 componentdidmount()

async componentDidMount() {
    await this.checkPermission(() => {
      navigator.geolocation.getCurrentPosition(
          (position)=>{
            this.fetchProfileStatus(position);
          },
          error => {
            if (error.code) {
              console.log(error)
              this.props.navigation.goBack();
            }
          },
          Platform.OS === 'android' ? {} :{enableHighAccuracy: true, timeout: 20000 }
        );
      });
}

谢谢

0 个答案:

没有答案