在 iOS 使用 Flutter 授予位置权限
Granting location permission with Flutter at iOS
下面的代码 运行 在 Android 处顺利,但卡在 iOS,当我点击地图图标时,系统应该更新我的位置:
import 'package:flutter/material.dart';
import 'package:location/location.dart';
import 'package:flutter/services.dart';
import 'package:simple_permissions/simple_permissions.dart';
class LocationState extends State {
String _loca_ext;
Location _location = new Location();
Map<String, double> _currentLocation;
String error;
@override
void initState() {
super.initState();
setState(() {
_loca_ext = 'Clik to update location';
});
}
// Platform messages are asynchronous, so we initialize in an async method.
_getLocation() async {
Map<String, double> location;
// Platform messages may fail, so we use a try/catch PlatformException.
try {
await SimplePermissions.requestPermission(Permission.AccessFineLocation);
location = await _location.getLocation();
error = null;
} on PlatformException catch (e) {
if (e.code == 'PERMISSION_DENIED') {
error = 'Permission denied';
} else if (e.code == 'PERMISSION_DENIED_NEVER_ASK') {
error =
'Permission denied - please ask the user to enable it from the app settings';
}
location = null;
}
print("error $error");
setState(() {
_currentLocation = location;
_loca_ext = ('${_currentLocation["latitude"]}, ${_currentLocation["longitude"]}' ?? 'Grant location Access');
print(_currentLocation);
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Baby Name Votes')),
body: _buildBody(context),
);
}
Widget _buildBody(BuildContext context) {
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 8.0),
child: Container(
decoration: BoxDecoration(
border: Border.all(color: Colors.grey),
borderRadius: BorderRadius.circular(5.0),
),
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
ButtonTheme.bar(
child: ButtonBar(
children: <Widget>[
Text.rich(
TextSpan(text: '$_loca_ext'),
),
FlatButton(
child: const Icon(Icons.map),
onPressed: () {
_getLocation();
var alt = _currentLocation["latitude"];
print("my $alt at location is: $_currentLocation");
},
)
])
)
]),
),
);
}
}
使用 iOS,出现以下错误:
error Permission denied
知道在 Info.plist
中,我添加了以下内容:
<key>NSLocationWhenInUseUsageDescription</key>
<string>This app needs access to location in the background.</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>This app needs access to location when in the background.</string>
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>This app needs access to location when open and in the background.</string>
注意
如果我在 settings/privacy/location
中手动授予它,它会起作用,但我需要以编程方式进行,或指导用户进行。
您尝试过使用 https://pub.dev/packages/permission 吗?
示例方法:
import 'package:permission/permission.dart';
var permissions = await Permission.getPermissionsStatus([PermissionName.Calendar, PermissionName.Camera]);
var permissionNames = await Permission.requestPermissions([PermissionName.Calendar, PermissionName.Camera]);
Permission.openSettings;
但是就像评论说的那样,如果权限被拒绝,那么您必须手动转到设置以启用它。如需更多了解,请参阅 medium/Requesting Location Permissions in iOS(2018 年 3 月)
Users have only one chance to respond to these alerts, so you want to
make your request at the right time and provide a good reason for it.
If the user doesn’t allow access, they’ll need to go to the settings
to enable location access.
下面的代码 运行 在 Android 处顺利,但卡在 iOS,当我点击地图图标时,系统应该更新我的位置:
import 'package:flutter/material.dart';
import 'package:location/location.dart';
import 'package:flutter/services.dart';
import 'package:simple_permissions/simple_permissions.dart';
class LocationState extends State {
String _loca_ext;
Location _location = new Location();
Map<String, double> _currentLocation;
String error;
@override
void initState() {
super.initState();
setState(() {
_loca_ext = 'Clik to update location';
});
}
// Platform messages are asynchronous, so we initialize in an async method.
_getLocation() async {
Map<String, double> location;
// Platform messages may fail, so we use a try/catch PlatformException.
try {
await SimplePermissions.requestPermission(Permission.AccessFineLocation);
location = await _location.getLocation();
error = null;
} on PlatformException catch (e) {
if (e.code == 'PERMISSION_DENIED') {
error = 'Permission denied';
} else if (e.code == 'PERMISSION_DENIED_NEVER_ASK') {
error =
'Permission denied - please ask the user to enable it from the app settings';
}
location = null;
}
print("error $error");
setState(() {
_currentLocation = location;
_loca_ext = ('${_currentLocation["latitude"]}, ${_currentLocation["longitude"]}' ?? 'Grant location Access');
print(_currentLocation);
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Baby Name Votes')),
body: _buildBody(context),
);
}
Widget _buildBody(BuildContext context) {
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 8.0),
child: Container(
decoration: BoxDecoration(
border: Border.all(color: Colors.grey),
borderRadius: BorderRadius.circular(5.0),
),
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
ButtonTheme.bar(
child: ButtonBar(
children: <Widget>[
Text.rich(
TextSpan(text: '$_loca_ext'),
),
FlatButton(
child: const Icon(Icons.map),
onPressed: () {
_getLocation();
var alt = _currentLocation["latitude"];
print("my $alt at location is: $_currentLocation");
},
)
])
)
]),
),
);
}
}
使用 iOS,出现以下错误:
error Permission denied
知道在 Info.plist
中,我添加了以下内容:
<key>NSLocationWhenInUseUsageDescription</key>
<string>This app needs access to location in the background.</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>This app needs access to location when in the background.</string>
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>This app needs access to location when open and in the background.</string>
注意
如果我在 settings/privacy/location
中手动授予它,它会起作用,但我需要以编程方式进行,或指导用户进行。
您尝试过使用 https://pub.dev/packages/permission 吗?
示例方法:
import 'package:permission/permission.dart';
var permissions = await Permission.getPermissionsStatus([PermissionName.Calendar, PermissionName.Camera]);
var permissionNames = await Permission.requestPermissions([PermissionName.Calendar, PermissionName.Camera]);
Permission.openSettings;
但是就像评论说的那样,如果权限被拒绝,那么您必须手动转到设置以启用它。如需更多了解,请参阅 medium/Requesting Location Permissions in iOS(2018 年 3 月)
Users have only one chance to respond to these alerts, so you want to make your request at the right time and provide a good reason for it. If the user doesn’t allow access, they’ll need to go to the settings to enable location access.