翩翩iOS请求位置权限不提示任何内容

问题描述 投票:0回答:1

我试图在我正在工作的应用程序上请求位置权限。它在Android设备上工作得很完美,但在iOS上没有任何提示。目标iOS版本是11,我使用的包是 geolocator: ^5.3.1permission_handler: ^5.0.0+hotfix.5.

这是我的代码。

这是我的实用函数,用于在状态不符合要求的情况下申请权限。granted.

Future<PermissionStatus> getDeviceLocationPermission() async {
 final PermissionStatus permissionStatus =
  await Permission.locationWhenInUse.status;

  if (permissionStatus != PermissionStatus.granted) {
    await Permission.locationWhenInUse.request();
  }

  return permissionStatus;
}

这是我用来收集位置细节的实用函数。

Future<Position> getDeviceCurrentLocation(
    {@required PermissionStatus permissionStatus}) async {
  Position position;
  Geolocator geolocator = Geolocator()..forceAndroidLocationManager;

  if (permissionStatus == null) {
    return null;
  }

  if (permissionStatus == PermissionStatus.granted) {
    position = await geolocator.getCurrentPosition(
        desiredAccuracy: LocationAccuracy.high);
  }

  return position;
}

这是我在有状态widget中访问这些实用程序的方式。

getDeviceLocationPermission().then((permissionStatus) {
      print(permissionStatus);
      getDeviceCurrentLocation(permissionStatus: permissionStatus)
          .then((position) {
        print(position);
      });
    });

这是我的info.plist对位置服务的权限。

<key>NSLocationWhenInUseUsageDescription</key>
<string>This app needs access to location when open.</string>
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>This app needs access to location when open and in the background.</string>

在iOS设备上,它不会提示一个询问允许或拒绝访问的对话框,也会返回一个 "允许 "或 "拒绝 "的对话框。null 值。

如果还需要其他信息,请问。谁能帮我解决这个问题?先谢谢你

flutter permissions location
1个回答
0
投票

我按照你的代码以及他们给出的包权限_handler中的代码。运行了这两段代码,重现了你代码中的问题。

问题可以解决,如下所示。:

创建一个通用的权限值列表.

List<Permission> lp = Permission.values;

调整函数getDeviceLocationPermission如下。

Future<PermissionStatus> getDeviceLocationPermission() async {

    Permission _permission = lp.elementAt(5); // 5 = location in use permission

    // The following line then prompts the required alert dialog
    final PermissionStatus permissionStatus = await _permission.request(); 


    /* final PermissionStatus permissionStatus = await Permission.locationWhenInUse.status;

    if (permissionStatus != PermissionStatus.granted) {
      await Permission.locationWhenInUse.request();
    }*/

    return permissionStatus;
}

注意:以上是解决你的问题所需的最小代码。根据你的需求,你可能需要取消if条件的注释。

在我看来,比较好用的软件包是 位置 包。一个包给了你权限处理程序以及latlongs,代码很直接。请看下面。

var location = new Location();
currentLocation = await location.getLocation(); // This prompts the dialog
// __If condition and all
debugPrint(currentLocation.latitude.toString());

0
投票

因为geolocator有_getLocationPermission()方法,这个方法会在内部获取位置权限。另外,我发现在ios模拟器中获取权限的方法不正确。发出 所以你可以像这样简化你的代码。

Future<Position> getDeviceCurrentLocation() async {
  Position position;
  Geolocator geolocator = Geolocator()..forceAndroidLocationManager;

  position = await geolocator.getCurrentPosition(
        desiredAccuracy: LocationAccuracy.high);


  return position;
}

and

getDeviceCurrentLocation()
          .then((position) {
        print(position);
      });
© www.soinside.com 2019 - 2024. All rights reserved.