我正在尝试申请我正在使用的应用程序的位置权限。它在Android设备上运行良好,但在iOS上不会提示任何信息。目标iOS版本是11,我使用的软件包是geolocator: ^5.3.1
和permission_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;
}
这就是我在有状态小部件中访问这些实用程序的方式。
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
值。
请询问是否需要其他信息。有人能帮我做这个吗?提前感谢!
-
将其添加到
info.plist
文件中。<key>NSLocationWhenInUseUsageDescription</key> <string>Location permission is required.</string>
-
将
PERMISSION_LOCATION=1
添加到您的podfile
中。检查以下内容:post_install do |installer| installer.pods_project.targets.each do |target| flutter_additional_ios_build_settings(target) target.build_configurations.each do |config| config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= [ '$(inherited)', ## Add this line 'PERMISSION_LOCATION=1' ] end end end
-
请求许可:
var status = await Permission.locationWhenInUse.request();
我遵循了您的代码以及他们在package permission_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条件的注释。
在我看来,最好使用的套餐是定位套餐。一个包为您提供了权限处理程序和latlong,代码非常直接。见下文:
var location = new Location();
currentLocation = await location.getLocation(); // This prompts the dialog
// __If condition and all
debugPrint(currentLocation.latitude.toString());
如果要使用地理定位器,则不需要权限permission_handler。因为geolocator有_getLocationPermission((方法,该方法将在内部获得定位权限。此外,我发现在ios模拟器中获取权限无法正常工作。你也可以检查一下这个问题。这样你就可以像这样简化你的代码:
Future<Position> getDeviceCurrentLocation() async {
Position position;
Geolocator geolocator = Geolocator()..forceAndroidLocationManager;
position = await geolocator.getCurrentPosition(
desiredAccuracy: LocationAccuracy.high);
return position;
}
和
getDeviceCurrentLocation()
.then((position) {
print(position);
});