You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

iOS中使用Geolocator请求位置权限失败问题求助

iOS端Geolocator权限始终返回permanentlyDenied,无权限弹窗(Android正常)

我使用的依赖版本为permission_handler: ^11.3.1和geolocator: ^13.0.1,Android端定位权限请求完全正常,但iOS端始终返回permission.permanentlyDenied,甚至不会弹出位置权限请求弹窗。

权限请求代码

Future<void> requestLocationAccess() async {
    final status = await Permission.location.request();

    if (status == PermissionStatus.permanentlyDenied) {
      Permission.location.onPermanentlyDeniedCallback(() {});
    }
    state = state.copyWith(location: status);

    _checkPermissionState(status); // 此方法会引导用户跳转到设置页面
  }

Info.plist文件内容

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>CFBundleDevelopmentRegion</key>
    <string>$(DEVELOPMENT_LANGUAGE)</string>
    <key>CFBundleDisplayName</key>
    <string>SAGA</string>
    <key>CFBundleExecutable</key>
    <string>$(EXECUTABLE_NAME)</string>
    <key>CFBundleIdentifier</key>
    <string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
    <key>CFBundleInfoDictionaryVersion</key>
    <string>6.0</string>
    <key>CFBundleName</key>
    <string>pjecz_saga_app</string>
    <key>CFBundlePackageType</key>
    <string>APPL</string>
    <key>CFBundleShortVersionString</key>
    <string>$(FLUTTER_BUILD_NAME)</string>
    <key>CFBundleSignature</key>
    <string>????</string>
    <key>CFBundleVersion</key>
    <string>$(FLUTTER_BUILD_NUMBER)</string>
    <key>LSRequiresIPhoneOS</key>
    <true/>
    <key>UILaunchStoryboardName</key>
    <string>LaunchScreen</string>
    <key>UIMainStoryboardFile</key>
    <string>Main</string>
    <key>UISupportedInterfaceOrientations</key>
    <array>
        <string>UIInterfaceOrientationPortrait</string>
        <string>UIInterfaceOrientationLandscapeLeft</string>
        <string>UIInterfaceOrientationLandscapeRight</string>
    </array>
    <key>UISupportedInterfaceOrientations~ipad</key>
    <array>
        <string>UIInterfaceOrientationPortrait</string>
        <string>UIInterfaceOrientationPortraitUpsideDown</string>
        <string>UIInterfaceOrientationLandscapeLeft</string>
        <string>UIInterfaceOrientationLandscapeRight</string>
    </array>
    <key>CADisableMinimumFrameDurationOnPhone</key>
    <true/>
    <key>UIApplicationSupportsIndirectInputEvents</key>
    <true/>
    <key>NSCameraUsageDescription</key>
    <string>Necesitamos acceso a la camara para tomar fotografías.</string>
    <key>NSPhotoLibraryUsageDescription</key>
    <string>Necesitamos acceso a la galería para seleccionar fotografías.</string>
    <key>NSAppleMusicUsageDescription</key>
    <string>Necesitamos acceso a la biblioteca multimedia</string>
    <key>NSLocationUsageDescription</key>
    <string>Necesitamos acceso a su ubicación para agregarlas a las fotografías.</string>
    <key>NSLocationAlwaysUsageDescription</key>
    <string>Necesitamos acceso a su ubicación para agregarlas a las fotografías.</string>
    <key>NSLocationWhenInUseUsageDescription</key>
    <string>Necesitamos acceso a su ubicación para agregarlas a las fotografías.</string>
    <key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
    <string>Necesitamos acceso a su ubicación para agregarlas a las fotografías.</string>
    <key>NSLocationTemporaryUsageDescriptionDictionary</key>
    <dict>
        <key>TemporaryPreciseAccuracy</key>
        <string>La aplicación requiere acceso a la ubicación precisa para vincularla con la fotografía.</string>
    </dict>
    <key>LSApplicationQueriesSchemes</key>
    <array>
    <string>sms</string>
    <string>tel</string>
    </array>
</dict>
</plist>

Podfile文件内容

# Uncomment this line to define a global platform for your project
platform :ios, '14.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__))
  target 'RunnerTests' do
    inherit! :search_paths
  end
end

# post_install do |installer|
#   installer.pods_project.targets.each do |target|
#     flutter_additional_ios_build_settings(target)
#     target.build_configurations.each do |config|
#       config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '14.0'
#     end
#   end
# end



# post_install do |installer|
#   installer.pods_project.targets.each do |target|
#     if target.name == "geolocator_apple"
#       target.build_configurations.each do |config|
#         config.build_settings ['GCC_PREPROCESSOR_DEFINITIONS'] ||= ['$(inherited)', 
#         'BYPASS_PERMISSION_LOCATION_ALWAYS=1',
#         'PERMISSION_CAMERA=1',
#         'PERMISSION_PHOTOS=1',
#         'PERMISSION_LOCATION_WHENINUSE=1',
#         'PERMISSION_MEDIA_LIBRARY=1'
#       ]
#       end
#     end
#     flutter_additional_ios_build_settings(target)
#   end
# end

# post_install do |installer|
#   installer.pods_project.targets.each do |target|
#     
#     # Permisos específicos para geolocator_apple
#     # if target.name == "geolocator_apple"
#     #   target.build_configurations.each do |config|
#     #     config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= ['$(inherited)',
#     #       'BYPASS_PERMISSION_LOCATION_ALWAYS=1',
#     #       'PERMISSION_LOCATION_WHENINUSE=1',
#     #       'PERMISSION_LOCATION=1',
#     #       'PERMISSION_CAMERA=1',
#     #       'PERMISSION_PHOTOS=1',
#     #       'PERMISSION_MEDIA_LIBRARY=1'
#     #     ]
#     #   end
#     # end

#     # Configuración común para todos los targets
#     target.build_configurations.each do |config|
#       config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '14.0'
#     end

#     # Ajustes adicionales de Flutter
#     flutter_additional_ios_build_settings(target)
#   end
# end


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

    target.build_configurations.each do |config|
      # You can remove unused permissions here
      # for more information: https://github.com/Baseflow/flutter-permission-handler/blob/main/permission_handler_apple/ios/Classes/PermissionHandlerEnums.h
      # e.g. when you don't need camera permission, just add 'PERMISSION_CAMERA=0'
      config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= [
        '$(inherited)',

        ## dart: PermissionGroup.calendar
        'PERMISSION_EVENTS=0',
        
        ## dart: PermissionGroup.calendarFullAccess
        'PERMISSION_EVENTS_FULL_ACCESS=0',

        ## dart: PermissionGroup.reminders
        'PERMISSION_REMINDERS=0',

        ## dart: PermissionGroup.contacts
        'PERMISSION_CONTACTS=0',

        ## dart: PermissionGroup.camera
        'PERMISSION_CAMERA=1',

        ## dart: PermissionGroup.microphone
        'PERMISSION_MICROPHONE=0',

        ## dart: PermissionGroup.speech
        'PERMISSION_SPEECH_RECOGNIZER=0',

        ## dart: PermissionGroup.photos
        'PERMISSION_PHOTOS=1',

        ## The 'PERMISSION_LOCATION' macro enables the `locationWhenInUse` and `locationAlways` permission. If
        ## the application only requires `locationWhenInUse`, only specify the `PERMISSION_LOCATION_WHENINUSE`
        ## macro.
        ##
        ## dart: [PermissionGroup.location, PermissionGroup.locationAlways, PermissionGroup.locationWhenInUse]
        'PERMISSION_LOCATION=1',
        'PERMISSION_LOCATION_WHENINUSE=0',

        ## dart: PermissionGroup.notification
        'PERMISSION_NOTIFICATIONS=0',

        ## dart: PermissionGroup.mediaLibrary
        'PERMISSION_MEDIA_LIBRARY=1',

        ## dart: PermissionGroup.sensors
        'PERMISSION_SENSORS=0',

        ## dart: PermissionGroup.bluetooth
        'PERMISSION_BLUETOOTH=0',

        ## dart: PermissionGroup.appTrackingTransparency
        'PERMISSION_APP_TRACKING_TRANSPARENCY=0',

        ## dart: PermissionGroup.criticalAlerts
        'PERMISSION_CRITICAL_ALERTS=0',

        ## dart: PermissionGroup.criticalAlerts
        'PERMISSION_ASSISTANT=0',
      ]

    end
  end
end

问题排查与解决步骤

1. 修正Podfile权限宏配置

当前Podfile中PERMISSION_LOCATION=1和PERMISSION_LOCATION_WHENINUSE=0的组合会导致权限逻辑冲突,根据你的需求调整:

  • 若只需使用时定位:
    'PERMISSION_LOCATION=0',
    'PERMISSION_LOCATION_WHENINUSE=1',
    
  • 若需始终允许定位:
    'PERMISSION_LOCATION=1',
    'PERMISSION_LOCATION_WHENINUSE=1',
    

2. 重置iOS权限缓存

iOS如果之前被用户永久拒绝过权限,会直接返回permanentlyDenied,需执行:

  • 删除模拟器/真机上的应用
  • 重启模拟器/真机
  • 重新安装应用测试

3. 明确权限请求类型

建议在代码中明确指定请求的权限类型,与Info.plist中的描述对应:

// 请求使用时定位
final status = await Permission.locationWhenInUse.request();
// 或请求始终定位
// final status = await Permission.locationAlways.request();

4. 清理重建项目

确保配置生效:

  1. 运行flutter clean
  2. 进入ios目录执行pod deintegrate && pod install
  3. 重新编译运行应用

内容的提问来源于stack exchange,提问作者day

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 06:39:52