【问题标题】:Flutter: Add location permission on IOS in permission handlerFlutter:在权限处理程序中添加IOS上的位置权限
【发布时间】:2021-01-25 04:53:51
【问题描述】:

我的 Flutter 项目有一个 Web 视图。在 Web 视图小部件中,我正在使用权限处理程序检查位置权限,如下所示:

  void _checkLocationPermission() async {
    if (await Permission.location.request().isGranted) {
      Position position = await getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
      if (position == null) {
        latitude = "";
        longitude = "";
      } else {
        latitude = position.latitude.toString();
        longitude = position.longitude.toString();
      }
      setState(() {
        isPermission = true;
      });
    }
  }

在 Info.plist 我添加了这些权限:

<key>NSLocationAlwaysUsageDescription</key>
<string>Needed to access location</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>Needed to access location</string>

这是 podfile:

# Uncomment this line to define a global platform for your project
# platform :ios, '9.0'

# CocoaPods analytics sends network stats synchronously affecting flutter build latency.
ENV['COCOAPODS_DISABLE_STATS'] = 'true'

project 'Runner', {
  'Debug' => :debug,
  'Profile' => :release,
  'Release' => :release,
}

def flutter_root
  generated_xcode_build_settings_path = File.expand_path(File.join('..', 'Flutter', 'Generated.xcconfig'), __FILE__)
  unless File.exist?(generated_xcode_build_settings_path)
    raise "#{generated_xcode_build_settings_path} must exist. If you're running pod install manually, make sure flutter pub get is executed first"
  end

  File.foreach(generated_xcode_build_settings_path) do |line|
    matches = line.match(/FLUTTER_ROOT\=(.*)/)
    return matches[1].strip if matches
  end
  raise "FLUTTER_ROOT not found in #{generated_xcode_build_settings_path}. Try deleting Generated.xcconfig, then run flutter pub get"
end

require File.expand_path(File.join('packages', 'flutter_tools', 'bin', 'podhelper'), flutter_root)

flutter_ios_podfile_setup

target 'Runner' do
  use_frameworks!
  use_modular_headers!

  flutter_install_all_ios_pods File.dirname(File.realpath(__FILE__))
end

#post_install do |installer|
#  installer.pods_project.targets.each do |target|
#    flutter_additional_ios_build_settings(target)
#  end
#end

post_install do |installer|
  installer.pods_project.targets.each do |target|
    target.build_configurations.each do |config|
      config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= [
        '$(inherited)',

        ## dart: [PermissionGroup.location, PermissionGroup.locationAlways, PermissionGroup.locationWhenInUse]
        'PERMISSION_LOCATION=0',

        ## dart: PermissionGroup.notification
        'PERMISSION_NOTIFICATIONS=0'
        ]
    end
  end
end

我评论了默认安装后:

#post_install do |installer|
#  installer.pods_project.targets.each do |target|
#    flutter_additional_ios_build_settings(target)
#  end
#end

根据权限处理程序suggestion,我在此文件末尾添加了这些行:

post_install do |installer|
  installer.pods_project.targets.each do |target|
    target.build_configurations.each do |config|
      config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= [
        '$(inherited)',

        ## dart: [PermissionGroup.location, PermissionGroup.locationAlways, PermissionGroup.locationWhenInUse]
        'PERMISSION_LOCATION=0',

        ## dart: PermissionGroup.notification
        'PERMISSION_NOTIFICATIONS=0'
        ]
    end
  end
end

但是当我在我的 iphone 7 plus 上运行时,当我打开我的 web 视图时,权限请求对话框没有显示并且我没有位置权限!!!

podfile 是否正确?因为这是我第一次在 podfile 中添加一些东西!!!

【问题讨论】:

    标签: ios flutter dart permissions


    【解决方案1】:

    您不需要权限处理程序,此功能可通过Location plugin查看他们的官方提供

    下载和导入插件

    location

    添加权限

    iOS 要在 iOS 中使用它,您必须在 Info.plist 中添加此权限:

    NSLocationWhenInUseUsageDescription
    NSLocationAlwaysUsageDescription
    

    欲了解更多信息,请查看official plugin

    初始化变量

      String latitude_data;
      String longitude_data;
      bool _serviceEnabled;
    

    当前位置函数

     Future _getLocation() async {
             
                Location location = new Location();
            
                var _permissionGranted = await location.hasPermission();
                _serviceEnabled = await location.serviceEnabled();
            
                if (_permissionGranted != PermissionStatus.granted || !_serviceEnabled) {
    ///asks permission and enable location dialogs
                  _permissionGranted = await location.requestPermission();
            
                  _serviceEnabled = await location.requestService();
            
                
                } else {
                ///Do something here
                }
            
                LocationData _currentPosition = await location.getLocation();
            
                longitude_data=_currentPosition.longitude.toString();
                latitude_data=_currentPosition.latitude.toString();
            
            ///if you want you can save data to sharedPrefrence   
             SharedPrefrence().setLatitude(_currentPosition.latitude.toString());
                SharedPrefrence().setLongitude(_currentPosition.longitude.toString());
            
              
              
              }
    

    然后从手机上卸载应用程序,运行flutter clean并再次运行

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-08-02
    • 2019-05-21
    • 2015-09-05
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    • 2021-11-28
    相关资源
    最近更新 更多