Sub topic 3
Declaring permissions in AndroidManifest.xml and Info.plist
This chapter assumes you can open the android and ios folders of a Flutter project, and that you know the basics of pubspec.yaml. It also assumes you have seen the runtime permission model from the earlier chapters: the user must grant location access while the app runs. Here you add the other half, the static declarations that make that runtime request possible.
Why a declaration is needed before the request
Both platforms use two gates for location access:
- Declaration time. The app states in a build-time file which sensitive capabilities it may ask for. The operating system reads this file at install time or when the app first requests access.
- Runtime. The user decides whether to grant each capability, usually through a system dialog.
A runtime request without a declaration fails, and Dart code cannot fix that. The Dart packages (geolocator, permission_handler) only call the native APIs. Those APIs check the declaration first.
| Platform | Declaration file | What it holds | Failure if missing |
|---|---|---|---|
| Android | `android/app/src/main/AndroidManifest.xml` | `<uses-permission>` entries | The request returns denied with no dialog, or reading a position throws a `SecurityException` |
| iOS | `ios/Runner/Info.plist` | Usage description strings | No dialog appears. Depending on the OS version and the API called, the app may be terminated |
Android: AndroidManifest.xml
Android groups location access into two permissions:
ACCESS_COARSE_LOCATIONgives an approximate position, typically within a few kilometers or a city block, depending on the device.ACCESS_FINE_LOCATIONgives a precise position from GPS and other sensors.
Declare both, even if you only need approximate location:
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:label="my_app"
android:name="${applicationName}"
android:icon="@mipmap/ic_launcher">
<!-- activities and meta-data stay as generated -->
</application>
</manifest>Why declare both
Since Android 12 (API level 31), the system dialog offers the user a choice between Precise and Approximate location. The dialog only appears in that form if the app declares both permissions. If you declare only ACCESS_FINE_LOCATION, the app fails to build or run correctly on recent targets, because Android requires fine location requests to include coarse as well. Declaring both is the safe default.
Placement rules
<uses-permission>elements are direct children of<manifest>. They go outside<application>.- Flutter projects have three manifests:
src/main,src/debug, andsrc/profile. The debug and profile ones contain only theINTERNETpermission for the Flutter tooling. Put location permissions insrc/main, so release builds include them.
What you do not need to declare
Normal runtime requests for foreground location need nothing else. ACCESS_BACKGROUND_LOCATION is a separate permission with its own rules, so leave it out until you build a feature that needs it. The background location chapter covers that.
iOS: Info.plist
iOS does not use a list of permission names. Instead, you provide a usage description: a human-readable sentence that iOS shows inside the permission dialog. The key you choose determines which kind of access you can request.
<key>NSLocationWhenInUseUsageDescription</key>
<string>We use your location to show nearby places while the app is open.</string>Place these <key>/<string> pairs inside the top-level <dict> of ios/Runner/Info.plist. A pair placed inside a nested <dict>, or a <key> without its <string>, is ignored or breaks the file.
The keys that matter
| Key | When you need it |
|---|---|
| `NSLocationWhenInUseUsageDescription` | Location while the app is in the foreground. This covers most apps. |
| `NSLocationAlwaysAndWhenInUseUsageDescription` | Requesting "Always" access, which includes background use. |
| `NSLocationTemporaryUsageDescriptionDictionary` | Asking for temporary full accuracy when the user chose approximate location. |
Older tutorials mention NSLocationAlwaysUsageDescription. It applies only to iOS 10 and earlier, so you can ignore it on any current deployment target.
If you request "Always" access, iOS expects both the always key and the when-in-use key to exist. Without the when-in-use key, the always request can fail silently. Add both whenever you plan to use background location.
Writing a good description
The string is shown to the user and reviewed by Apple. Follow these rules:
- Say what the feature does and why it needs location: "to show nearby places while the app is open".
- Do not write only "Location is required" or "Needed for app functionality". Apple can reject such text.
- Match the key to the behavior. A when-in-use string must not promise or imply background tracking.
- If the app ships in several languages, localize these strings in
InfoPlist.stringsfiles in Xcode. The plist value is the fallback.
You can edit the plist in a text editor, or open ios/Runner.xcworkspace in Xcode and add the keys in the Info tab, where they appear under friendly names such as "Privacy - Location When In Use Usage Description". Both ways write the same plist.
Extra setup for permission_handler on iOS
geolocator needs nothing beyond the plist keys. permission_handler is different. To keep apps from shipping code for permissions they do not use (which Apple's review can flag), it compiles each permission only if you enable a preprocessor flag. Add the flags to the post_install block of ios/Podfile:
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['GCC_PREPROCESSOR_DEFINITIONS'] ||= [
'$(inherited)',
'PERMISSION_LOCATION=1',
]
end
end
endThe exact macro names have changed between package versions, for example PERMISSION_LOCATION_WHENINUSE=1 for when-in-use only. Check the README of the version in your pubspec.lock. If you skip this step, Permission.locationWhenInUse.status always returns denied on iOS, even after the user taps Allow. This is a common source of confusion because the plist looks correct.
Applying the changes
Native files are not covered by hot reload or hot restart. After editing either file:
- Stop the app.
- Run
flutter runagain, orflutter cleanfollowed byflutter runif the change does not seem to apply. - On iOS, after changing the Podfile, run
pod installinsideios/or letflutter rundo it.
Permission state is also stored by the operating system, not by your app build. If you tested an earlier build, the device may remember "Denied". Uninstall the app, or reset it from the system settings, before retesting a new declaration.
Verifying the declarations
You can check the Android result without running the app by inspecting the merged manifest, which includes entries added by plugins:
cd android
./gradlew app:processDebugMainManifestThen open android/app/build/intermediates/merged_manifests/debug/ and look for the location uses-permission lines. A plugin may add permissions you did not write, so reviewing the merged file before release shows what users and Play review will see.
For iOS, a missing key usually shows up in the Xcode console when you request permission, with a message naming the key that is missing.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Android: `requestPermission()` returns denied at once, no dialog | `uses-permission` missing, or placed inside `<application>` | Move it to be a child of `<manifest>` in `src/main` |
| Android: dialog has no Precise/Approximate choice | Only one of the two location permissions declared | Declare both |
| iOS: no dialog, console names a missing key | Usage description key absent or in a nested dict | Add the key at the top level of the plist |
| iOS: `permission_handler` always reports denied | Podfile macro not set | Add the preprocessor flag and rebuild |
| Change has no effect | Only hot reload was used, or old permission state is stored | Do a full rebuild and reinstall the app |
Exercise
A teammate adds only ACCESS_FINE_LOCATION inside the <application> element of android/app/src/debug/AndroidManifest.xml. Name three problems with this declaration.
Show answer
First, the element is inside <application>, but <uses-permission> must be a direct child of <manifest>. Second, it is in the debug manifest, so release builds do not include it. It belongs in src/main. Third, it declares only the fine permission. Declare ACCESS_COARSE_LOCATION as well, so Android 12 and later can show the Precise/Approximate choice.
Exercise
Your iOS app shows no location dialog, and you confirmed that NSLocationWhenInUseUsageDescription is in the plist. You use permission_handler, and the status is always denied. What do you check next, and why?
Show answer
Check the ios/Podfile for the PERMISSION_LOCATION preprocessor flag in the post_install block (the exact macro name depends on the package version). permission_handler compiles each permission only when its flag is set, so without it the location code is absent and the status stays denied. After adding the flag, run pod install and do a full rebuild.