在iOS上使用Flutter授予位置权限

时间:2018-12-21 12:57:29

标签: ios permissions flutter location

下面的代码在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上,出现以下错误:

  

错误权限被拒绝

知道在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中手动授予了它,它就可以工作,但是我需要以编程方式进行操作,或者指导用户进行操作。

1 个答案:

答案 0 :(得分:0)

您是否尝试过使用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月)

  

用户只有一次响应这些警报的机会,因此您想   在正确的时间提出您的要求,并提供充分的理由。   如果用户不允许访问,则需要进入设置   启用位置访问。