javascript - react-native中iOS上的位置权限不起作用
问题描述
ios应用中没有弹出位置权限,也看不到应用设置中的权限选项
在安卓上运行良好。因为我可以使用 PermissionsAndroid 来获取权限。
通过查看其他答案,已经在 info.plist 中使用了以下选项。很少有答案只提到android。
info.plist
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>Location Permission</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>Location Permission</string>
<key>NSLocationUsageDescription</key>
<string>GPS data is required to...</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>Location Permission</string>
codeinthefile.js
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
{
title: "Geolocation Permission",
message: "App needs access to your phone's location.",
}
);
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
Geolocation.getCurrentPosition(
position => {
Geocoder.from({
latitude: position.coords.latitude,
longitude: position.coords.longitude,
})
.then(json => {
console.log(json);
})
.catch(error => {
console.log(error);
});
},
error => {
console.log(error);
},
{ enableHighAccuracy: true, timeout: 15000, maximumAge: 10000 }
);
} else {
console.log('Location permission not granted!);
}
} catch (err) {
console.log('Location permission not granted!)
}
如果通过使用 info.plist 中的上述值,我应该可以访问位置,那么不应该出现未授予权限的错误。
解决方案
不要在iOS中使用PermissionAndroid,将权限要求放在info.plist中就足够了,
尝试类似的东西,
if(Platform.OS === "ios"){
// your code using Geolocation and asking for authorisation with
geolocation.requestAuthorization()
}else{
// ask for PermissionAndroid as written in your code
}
推荐阅读
- javascript - 将对象数组的每个元素乘以标量
- php - 服务器中的 PHP 会话意外清除
- javascript - 不能在 Recompse 中使用 Props
- python - PythonAnywhere.com Flask Web 应用程序:没有名为“flask_app”的模块
- python-2.7 - 在python中分别处理重复值
- redis - 如何在集群环境中使用redis扫描?
- r - R xts 时间序列递归扩展
- angular - 动态组件和延迟加载,Angular Elements 能帮到我吗?
- javascript - 关闭浏览器后本地存储重置
- angular - 在 Angular 模块中延迟加载 JS