ios - Flutter:在权限处理程序中在IOS上添加位置权限

标签 ios flutter dart permissions

我在flutter项目中有一个网络 View 。在Web View 小部件内,我正在使用以下权限处理程序检查位置权限:

  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上运行并打开网页 View 时,未显示权限请求对话框,并且我没有位置权限!!!
podfile是正确的吗?因为这是我第一次在podfile中添加内容!

最佳答案

您不需要为此的权限处理程序,此功能可通过Location plugin checkout 其官方版本
下载并导入插件
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());
        
          
          
          }
然后从手机上卸载该应用,运行干净然后再次运行

关于ios - Flutter:在权限处理程序中在IOS上添加位置权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64291377/

相关文章:

ios - 用于 UIBarButtonSystemItemAdd 和其他的 minic iOS 7 UIBarButton 字体

ios - cocos2d 着色 Sprite 白色不透明

android - 单击时更新按钮的不透明度? flutter

debugging - 如何在Flutter web中调试或打印?

dart - 在dartlang中,dill和kernel snapshot有什么区别?

ios - 如何在 iOS 7 的 UITextView 中设置属性文本的颜色和对齐方式?

iphone - 在uiview上画直线

flutter - 如何解决 删除 play-services-safetynet 警告?

angularjs - Angular.Dart 中 ngTransclude 的模拟

flutter - 在同一行格式化函数参数