繁体   English   中英

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
}

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

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();


暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM