iOS上的位置权限在react-native中不起作用

时间:2019-07-19 14:07:25

标签: javascript react-native geolocation

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中使用上述值,我应该可以访问位置,那么就不会出现未授予权限的错误。

3 个答案:

答案 0 :(得分:1)

在下面同时使用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();


答案 1 :(得分:0)

在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
}

答案 2 :(得分:0)

谢谢道格和戴维,根据您的建议,我按照以下对我有用的方式对代码进行了更改:

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!!!!');

    }

}