Sub topic 1
How runtime permissions work on Android and iOS
Location is one of the most sensitive pieces of data a phone holds. Both Android and iOS therefore stop an app from reading it until two separate conditions are met. The app must declare that it wants location, and the user must grant it while the app is running. This chapter explains that model. Later chapters build the Flutter code on top of it.
Prerequisites
This chapter assumes you can:
- Write basic Dart and build Flutter widgets.
- Use
async/awaitandFuturein Dart. - Add a package with
flutter pub addand editpubspec.yaml. - Open the
androidandiosfolders of a Flutter project.
No location code runs in this chapter. You will mainly read and edit two platform files.
Why there are two gates
Permissions on mobile follow a two-step design.
- Declaration (build time). The app lists the permissions it may ask for in a platform file. Android uses
AndroidManifest.xml. iOS usesInfo.plist. The operating system reads these files when the app is installed or launched. - Grant (run time). When the app first needs the data, it asks the operating system to show a dialog. The user chooses what to allow. The app can only read location after that choice.
Both steps are required. A declaration alone gives the app no access. A runtime request without a declaration fails. On iOS the app can crash if the usage description is missing. On Android the request is silently denied.
The runtime step exists because users should be able to decide at the moment of use, and to change their mind later. Permission is not a one-time setting. The user can revoke it in system settings at any time, even while your app is in the background. Your code must therefore check the current state each time it needs location. It cannot assume that a past grant still holds.
How Android handles location permission
Android defines two location permissions, and they differ in precision.
| Permission | What it gives |
|---|---|
| `ACCESS_COARSE_LOCATION` | Approximate location, roughly a city block |
| `ACCESS_FINE_LOCATION` | Precise location, from GPS and similar sources |
Both are "dangerous" permissions in Android's terms. This means the system requires a runtime dialog for them. Declaring them in the manifest is necessary but not enough.
Since Android 12 (API level 31), the dialog lets the user pick between Precise and Approximate. If your app declares both permissions, the user may grant only the approximate one. Your code must work with whichever one it receives. A later chapter covers precision in detail.
Android also offers choices about duration. In recent versions the dialog usually offers:
- While using the app: access only when the app is visible or running a foreground service.
- Only this time: access that ends when the user leaves the app.
- Don't allow: no access.
Android also has a concept of a user denying twice. After the user denies the same permission twice, the system stops showing the dialog and returns "denied" immediately. This is the Android form of permanent denial. Your app can only recover by sending the user to system settings.
How iOS handles location permission
iOS has a different shape. Instead of listing permissions in a manifest, you add usage description strings to Info.plist. The system shows the string inside the permission dialog, so it must explain the purpose in plain language.
| Key | When it applies |
|---|---|
| `NSLocationWhenInUseUsageDescription` | Location while the app is in use |
| `NSLocationAlwaysAndWhenInUseUsageDescription` | Location also in the background |
The user's options on iOS are:
- Allow While Using App
- Allow Once
- Don't Allow
Two differences from Android matter for your code.
First, iOS shows the system dialog only once per install for the initial choice. If the user taps Don't Allow, later requests do nothing. The user must change the setting manually. From the app's side, this looks like permanent denial straight away.
Second, iOS lets the user choose Precise Location on or off in the same dialog (iOS 14 and later). With it off, the app receives an approximate position.
Comparing the two platforms
| Topic | Android | iOS |
|---|---|---|
| Where you declare | `AndroidManifest.xml` | `Info.plist` |
| What you declare | Permission names | Usage description text |
| Precision choice | Coarse and fine permissions | Precise toggle in the dialog |
| Retry after denial | Allowed once more, then blocked | Not allowed after first denial |
| Recovery from block | Open app settings | Open app settings |
| Missing declaration | Request is silently denied | App can crash or the dialog never appears |
Keep this table in mind. The Flutter packages hide many differences, but not all of them. A flow that works on Android can behave differently on iOS because of the "ask once" rule.
The declarations
Android needs the two permission entries inside the <manifest> element of android/app/src/main/AndroidManifest.xml. Put them outside the <application> element.
<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 ...>
...
</application>
</manifest>The ... marks code your project already contains. Leave it as it is.
iOS needs a key and a string in ios/Runner/Info.plist. Add them inside the top-level <dict>.
<key>NSLocationWhenInUseUsageDescription</key>
<string>We use your location to show nearby places while the app is open.</string>Write the string for a user, not a developer. It should say what you do with the location and when. Reviewers and users both read it.
The permission lifecycle
A permission is not just "granted" or "not granted". Over the life of an install it passes through several states. The next chapters treat them in detail. For now, think of the lifecycle as follows.
The diagram makes three points that shape your code:
- The state can change without your app running. The user may open Settings and flip it.
- "Denied" and "permanently denied" call for different responses. The first lets you ask again. The second needs a path to Settings.
- The only way out of permanent denial is a user action outside your app.
Location services versus permission
There is a second, separate switch. The phone has a system-wide location services toggle. It turns the location hardware and system features on or off for the whole device. App permission is per app. Services are per device.
An app can hold full permission and still get no position because the toggle is off. A user can also have services on and have denied your app. These are different problems with different fixes:
| Situation | Fix |
|---|---|
| Location services are off | Ask the user to turn on location in system settings |
| Permission is denied | Ask again, or send the user to app settings |
Your code should check both, and tell the user which one is the problem. A message saying "location is unavailable" without cause sends the user to the wrong settings screen.
Common mistakes
- Declaring only one platform. Developers who test on Android forget
Info.plist, then see the iOS request fail. - Requesting at launch. Asking before the user sees any reason for location lowers the grant rate. Ask when the feature is first used.
- Caching the result. Storing "granted" in a variable and trusting it later ignores revocation. Check again when you need the data.
- Treating denial as an error. Denial is a normal outcome. Design a useful screen for it.
- Testing only the happy path. Test denial, permanent denial and services off on a real device or emulator.
Summary of the model
- Location needs a declaration and a runtime grant.
- Android declares permission names. iOS declares usage description text.
- The user can choose precise or approximate location on both platforms.
- Denial can become permanent, and recovery then needs system settings.
- The location services switch is separate from app permission.
- The state can change at any time, so check it each time.
Exercise
A user installs your app on iOS, taps "Don't Allow" on the location dialog, and later taps your "Use my location" button. Your code calls the request function again. What happens, and what should the app do next?
Show answer
iOS does not show the dialog a second time. The request returns a denied result straight away, which the app should treat as permanent denial. The app should explain why location helps and offer a button that opens the app's page in system Settings, where the user can change the permission.
Exercise
Your app declares ACCESSFINELOCATION and a user has granted it, but the app still receives no position. Name one cause that is not about app permission, and say how you would tell the user to fix it.
Show answer
The device's location services switch may be off. This is a system-wide setting separate from the app permission. The app should detect it and tell the user to turn on location in the device's system settings.