Skip to content

Update GPS and location permission handling for Android 12+ and iOS changes #6

@jimmckeeth

Description

@jimmckeeth

Overview

Location permission handling has changed substantially on both Android and iOS since 2018. The training code requests ACCESS_FINE_LOCATION using the old permission model; this needs updating to handle precise vs. approximate location (Android 12+), the separate background location permission (Android 10+), and updated iOS location authorization flows.

Background

Android Changes

API Level Change
Android 10 (API 29) ACCESS_BACKGROUND_LOCATION introduced as a separate permission
Android 12 (API 31) Users can choose Approximate (COARSE) vs Precise (FINE) location even when FINE is requested
Android 12 (API 31) Apps must request COARSE and FINE together to show the choice dialog
Android 13 (API 33) Background location requires showing a rationale before sending user to Settings

The current code requests only ACCESS_FINE_LOCATION. On Android 12+ the user sees a dialog offering both Precise and Approximate options, but the code doesn't handle the case where Approximate is chosen — TLocationSensor will still receive coordinates but with reduced accuracy (~3 km radius).

iOS Changes

  • iOS 14: CLLocationManager added accuracyAuthorization (.fullAccuracy vs .reducedAccuracy). Apps should request full accuracy with a purpose key (NSLocationDefaultAccuracyReduced) only when needed.
  • "Allow Once" authorization added in iOS 13 — the app must handle single-use location grants.
  • NSLocationUsageDescription (pre-iOS 8 key) should be removed; use NSLocationWhenInUseUsageDescription only.
  • Background location (NSLocationAlwaysUsageDescription) should be removed if the app only needs foreground location.

Current Code

In formMain.pas and sensor-related frames, TLocationSensor is enabled and OnLocationChanged fires. The permission is checked/requested via direct JNI (TJManifest_permission.JavaClass.ACCESS_FINE_LOCATION) and System.Permissions. The code does not distinguish between precise and approximate accuracy.

Files Affected

lab-src/Lab04.../forms/formMain.pas
lab-src/Lab05.../frames/uNewEntryFrame.pas
lab-src/Lab*/frames/uEntryDetailsFrame.pas
lab-src/Lab*/AndroidManifest.template.xml
Info.plist (all labs, via IDE plist editor)

Steps to Address

  1. Update runtime permission request to include both ACCESS_COARSE_LOCATION and ACCESS_FINE_LOCATION (required pair on Android 12+ to show the precision choice dialog).
  2. In the OnLocationChanged handler, check TLocationSensor.Accuracy and display a UI indicator or warning if only approximate location is available.
  3. Remove ACCESS_BACKGROUND_LOCATION from the manifest — FieldLogger only needs foreground location.
  4. On iOS, remove NSLocationAlwaysUsageDescription from Info.plist if present; keep only NSLocationWhenInUseUsageDescription.
  5. Handle the CLAuthorizationStatus.authorizedWhenInUse vs .authorizedAlways distinction — FieldLogger should only request "When In Use."
  6. Handle the iOS reducedAccuracy case gracefully (same as Android approximate location).
  7. Update lab instructions to explain the new dual-permission model and precision choice.

Test Plan

  • On Android 12 emulator, granting Precise location: GPS coordinates appear in the entry details frame with expected accuracy.
  • On Android 12 emulator, granting Approximate location: app shows a notice that reduced accuracy is active; no crash occurs.
  • On Android 10 emulator: no ACCESS_BACKGROUND_LOCATION is requested.
  • Denying location permission entirely: app continues to function; location fields show "unavailable."
  • On iOS 17 simulator, the permission prompt uses NSLocationWhenInUseUsageDescription text.
  • Choosing "Allow Once" on iOS: location is captured for the current session; next session prompts again.
  • TLocationSensor correctly stops updates when the entry capture screen is dismissed.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions