我正在尝试在我的本机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 }
);
});
}
谢谢