【发布时间】:2019-07-19 14:07:25
【问题描述】:
ios应用中没有弹出位置权限,也看不到应用设置中的权限选项
在 Android 上运行良好。因为我可以使用 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 中的上述值,我应该可以访问位置,那么不应该出现未授予权限的错误。
【问题讨论】:
-
感谢@Andrew 的回复。我也检查了这个库。但是这个库在做什么,默认情况下不能在 react-native 中完成?此外,我必须将异步存储与库一起使用,而不是总是询问权限。似乎不是一个合适的解决方案。
-
'react-native-permissions' 不会再询问您是否同意该权限。
标签: javascript react-native geolocation