【问题标题】:Location permission on iOS in react-native not workingreact-native中iOS上的位置权限不起作用
【发布时间】:2019-07-19 14:07:25
【问题描述】:

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

【问题讨论】:

  • 感谢@Andrew 的回复。我也检查了这个库。但是这个库在做什么,默认情况下不能在 react-native 中完成?此外,我必须将异步存储与库一起使用,而不是总是询问权限。似乎不是一个合适的解决方案。
  • 'react-native-permissions' 不会再询问您是否同意该权限。

标签: javascript react-native geolocation


【解决方案1】:

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
}

【讨论】:

  • 对于 ios,您使用 React Native Geolocation 请求geolocation.requestAuthorization(); 的权限。这将请求 info.plist 中设置的权限级别。来自文档:facebook.github.io/react-native/docs/0.59/geolocation
  • 是的,完全正确。他遇到的问题是在 iOS 中使用 PermissionsAndroid ,可能返回 null 。相反,他必须直接使用地理定位,请求授权。
  • 我还是有问题
【解决方案2】:

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

    }

}

【讨论】:

  • 这个在ios模拟器上能用吗,还是应该用真机测试?
  • 不,它在模拟器上不起作用,你必须使用实际的设备。
  • 如何检查是否在 iOS 上授予权限?
  • 您必须在 Xcode 的 info.plist 中设置 Always 和 When in Usage 键,当您签入上述代码时,这将给出真实值。
【解决方案3】:

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


【讨论】:

    猜你喜欢
    • 2019-05-13
    • 2022-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-23
    • 2018-07-18
    相关资源
    最近更新 更多