我是移動開發的新手。我正在嘗試在運行 IOS 14.4 的 ios 模擬器中訪問我當前的位置,即使按照此平臺上的一些答案的建議在模擬器中設定了自定義位置,我仍然收到“位置權限被拒絕”錯誤, here。我正在運行的代碼是這個
Geolocation.getCurrentPosition(
position => {
this.setState({
latitude: position.coords.latitude,
longitude: position.coords.longitude,
coordinates: this.state.coordinates.concat({
latitude: position.coords.latitude,
longitude: position.coords.longitude
})
});
},
error => {
Alert.alert(error.message.toString());
},
{
showLocationDialog: true,
enableHighAccuracy: true,
timeout: 20000,
maximumAge: 0
}
);
我已經在我的 info-plist 中設定了這些權限。
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>Description</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>Will you allow this app to always know your location?</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>Do you allow this app to know your current location?</string>
我不完全確定我在這里可能做錯了什么。這是當前嘗試訪問該位置的唯一代碼。
即使在將當前位置設定為Features->Location->customlocation中的自定義位置之后,是否還有其他原因導致此錯誤仍然存??在?正如該平臺上的其他答案所建議的那樣。
uj5u.com熱心網友回復:
在運行需要位置訪問的代碼之前,需要在運行時詢問位置訪問權限。
react-native-permissions是跨平臺權限的標準庫
按照此處的安裝說明 - https://www.npmjs.com/package/react-native-permissions
import { request, PERMISSIONS, RESULT } from "react-native-permissions";
function getUserLocation() {
Geolocation.getCurrentPosition(
(position) => {
this.setState({
latitude: position.coords.latitude,
longitude: position.coords.longitude,
coordinates: this.state.coordinates.concat({
latitude: position.coords.latitude,
longitude: position.coords.longitude,
}),
});
},
(error) => {
Alert.alert(error.message.toString());
},
{
showLocationDialog: true,
enableHighAccuracy: true,
timeout: 20000,
maximumAge: 0,
}
);
}
request(PERMISSIONS.IOS.LOCATION_ALWAYS).then((result) => {
switch (result) {
case RESULTS.UNAVAILABLE:
console.log(
"This feature is not available (on this device / in this context)"
);
break;
case RESULTS.DENIED:
console.log(
"The permission has not been requested / is denied but requestable"
);
break;
case RESULTS.LIMITED:
console.log("The permission is limited: some actions are possible");
break;
case RESULTS.GRANTED:
console.log("The permission is granted");
// Permission has been granted - app can request location coordinates
getUserLocation();
break;
case RESULTS.BLOCKED:
console.log("The permission is denied and not requestable anymore");
break;
}
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/444171.html
上一篇:從相機讀取錯誤:PlatformException(CAMERA_ERROR,QrReadercould'topencamera,null,null)oniOSwithqr_mobile_v
