【发布时间】:2020-02-18 10:03:54
【问题描述】:
我使用 react-native-community/geolocation 来检查用户位置。一切都只能在android中正常工作。位置在启动时使用,因此在启动时会在 android 弹出消息中提示。 在 iOS 上,我没有弹出请求权限。
为了检查权限,我安装了 react-native-location 我的测试代码返回 '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>
【问题讨论】:
标签: react-native