0

I have a web view on my flutter project. Inside web view widget i am checking location permission with permission handler like below:

  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;
      });
    }
  }

In Info.plist i added these permissions:

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

And this is 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

I commented default post install :

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

and I added these lines at the end of this file, according permission handler 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

But when i run on my iphone 7 plus when i open my web view the permission request dialog not shown and i don't have location permission!!!

podfile is correct? Because this is a first time i added something in podfile!!!

Maz341
  • 2,232
  • 15
  • 20
Cyrus the Great
  • 5,145
  • 5
  • 68
  • 149

2 Answers2

2

You don't need permission handler for this,this feature is available with Location plugin checkout their official

Download and import plugin

location

Add Permission

iOS And to use it in iOS, you have to add this permission in Info.plist :

NSLocationWhenInUseUsageDescription
NSLocationAlwaysUsageDescription

For more info checkout official plugin

Initialize variables

  String latitude_data;
  String longitude_data;
  bool _serviceEnabled;

Current Location Function

 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());
        
          
          
          }

Then uninstall the app from phone,run flutter clean and run again

Abhijith
  • 2,227
  • 2
  • 15
  • 39
  • Yes, I found out that geolocator don't need to permission handler. Thanks Anyway. – Cyrus the Great Oct 10 '20 at 11:37
  • It didn't work on web unfortunately https://stackoverflow.com/questions/64411270/flutter-web-get-location-on-web-by-location-plugin @Assassin – Cyrus the Great Oct 19 '20 at 13:33
  • for setup location in web follow this https://dev.to/aseemwangoo/flutter-web-and-location-2l1@sayreskabir – Abhijith Oct 19 '20 at 13:39
  • At the end of my question https://stackoverflow.com/questions/64411270/flutter-web-get-location-on-web-by-location-plugin , I mentioned that i use Mozilla location, but it is not work too @Assassin – Cyrus the Great Oct 19 '20 at 15:06
0

Actually you added the values in Podfile backwards. You need to assign them "1" for them to be allowed. Now you have actually denied them: 'PERMISSION_LOCATION=1', 'PERMISSION_NOTIFICATIONS=1'