我使用react-native-community/geolocation来检查用户位置。只有在安卓系统中才能正常工作。位置在启动时使用,因此android中的弹出消息在启动时被提示。在iOS上,我没有弹出请求许可的窗口。
为了检查权限,我安装了react-native-location I,我的测试代码返回'notDetermined‘
我的代码:
import Geolocation from '@react-native-community/geolocation';
import RNLocation, {getCurrentPermission} from 'react-native-location';
...
if(Platform.OS === 'ios'){
Geolocation.requestAuthorization();
const locationConfig = {authorizationLevel: "whenInUse"}; //try "always" too
Geolocation.setRNConfiguration(locationConfig);
}
...
RNLocation.configure({
distanceFilter: 1.0
});
RNLocation.getCurrentPermission().then(curPerr => {
console.log(curPerr); //result notDetermined
});
RNLocation.requestPermission({
ios: "whenInUse",
android: {
detail: "coarse"
}
}).then(granted => {
console.log('granted: ', granted); //it doesn't show it in the console!
if (granted) {
this.locationSubscription = RNLocation.subscribeToLocationUpdates(locations => {
})
}
})Info.plist
<key>NSLocationAlwaysUsageDescription</key>
<string>We use your location...</string>
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>We use your location...</string>
<key>NSLocationWhenInUseUsageDescription </key>
<string>We use your location...</string>
...
<key>UIBackgroundModes</key>
<array>
<string>fetch</string>
<string>location</string>
</array>发布于 2020-03-06 16:53:50
我确实像你一样写过Info.plist文件,也遇到过同样的情况。在我的例子中,我删除了第一个和第二个选项,只保留了第三个选项,然后它就起作用了。
<key>NSLocationWhenInUseUsageDescription</key>
<string>We use your location...</string>ios版本是12。我不是很确定,但这个Q&A与您的问题相关,可能会有所帮助。
https://stackoverflow.com/questions/60278279
复制相似问题