簡體   English   中英

Flutter:在權限處理程序中添加對IOS的定位權限

[英]Flutter: Add location permission on IOS in permission handler

我的 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>

這是播客文件:

# 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

根據權限處理程序的建議,我在此文件的末尾添加了這些行:

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 中添加內容!!!

您不需要權限處理程序,此功能可通過Location plugin他們的官方

下載並導入插件

地點

添加權限

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

NSLocationWhenInUseUsageDescription
NSLocationAlwaysUsageDescription

有關更多信息,請查看官方插件

初始化變量

  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 並再次運行

實際上,您向后添加了 Podfile 中的值。 您需要為它們分配“1”才能允許它們。 現在你實際上已經拒絕了他們: 'PERMISSION_LOCATION=1', 'PERMISSION_NOTIFICATIONS=1'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM