react-native 中 iOS 的位置权限不起作用
Location permission on iOS in react-native not working
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中使用上述值,我应该可以访问位置,那么应该不会出现未授予权限的错误。
不要在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
}
谢谢 Doug 和 David,根据您的建议,我按照以下对我有用的方式修改了我的代码:
if(Platform.OS === 'ios'){
Geolocation.requestAuthorization();
this.getGeoLocation();
}else {
let granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
{
title: "App Geolocation Permission",
message: "App needs access to your phone's location.",
}
);
if (androidGranted === PermissionsAndroid.RESULTS.GRANTED) {
this.getGeoLocation();
} else {
console.log('Location permission not granted!!!!');
}
}
使用下面的 android 和 ios 权限。
import {Platform} from 'react-native';
import {request, PERMISSIONS, RESULTS} from 'react-native-permissions';
....
export async function getLocationPermissions() {
const granted = await request(
Platform.select({
android: PERMISSIONS.ANDROID.ACCESS_COARSE_LOCATION,
ios: PERMISSIONS.IOS.LOCATION_WHEN_IN_USE,
}),
{
title: 'DemoApp',
message: 'DemoApp would like access to your location ',
},
);
return granted === RESULTS.GRANTED;
}
....
// usage
const granted = await getLocationPermissions();
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中使用上述值,我应该可以访问位置,那么应该不会出现未授予权限的错误。
不要在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
}
谢谢 Doug 和 David,根据您的建议,我按照以下对我有用的方式修改了我的代码:
if(Platform.OS === 'ios'){
Geolocation.requestAuthorization();
this.getGeoLocation();
}else {
let granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
{
title: "App Geolocation Permission",
message: "App needs access to your phone's location.",
}
);
if (androidGranted === PermissionsAndroid.RESULTS.GRANTED) {
this.getGeoLocation();
} else {
console.log('Location permission not granted!!!!');
}
}
使用下面的 android 和 ios 权限。
import {Platform} from 'react-native';
import {request, PERMISSIONS, RESULTS} from 'react-native-permissions';
....
export async function getLocationPermissions() {
const granted = await request(
Platform.select({
android: PERMISSIONS.ANDROID.ACCESS_COARSE_LOCATION,
ios: PERMISSIONS.IOS.LOCATION_WHEN_IN_USE,
}),
{
title: 'DemoApp',
message: 'DemoApp would like access to your location ',
},
);
return granted === RESULTS.GRANTED;
}
....
// usage
const granted = await getLocationPermissions();