Skip to content

fix(iOS): resolve permission dialog hang when app goes to background.#1774

Open
guide-linhnv wants to merge 1 commit intoBaseflow:mainfrom
guide-inc-org:fix/permission-dialog-dismiss-ios
Open

fix(iOS): resolve permission dialog hang when app goes to background.#1774
guide-linhnv wants to merge 1 commit intoBaseflow:mainfrom
guide-inc-org:fix/permission-dialog-dismiss-ios

Conversation

@guide-linhnv
Copy link

@guide-linhnv guide-linhnv commented Mar 12, 2026

Replace this paragraph with a short description of what issue this pull request (PR) solves and provide a description of the change. Consider including before/after screenshots.

List at least one fixed issue.

Pre-launch Checklist

  • I made sure the project builds.
  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I updated pubspec.yaml with an appropriate new version according to the pub versioning philosophy, or this PR is does not need version changes.
  • I updated CHANGELOG.md to add a description of the change.
  • I updated/added relevant documentation (doc comments with ///).
  • I rebased onto main.
  • I added new tests to check the change I am making, or this PR does not need tests.
  • I made sure all existing and new tests are passing.
  • I ran dart format . and committed any changes.
  • I ran flutter analyze and fixed any errors.

@guide-linhnv guide-linhnv changed the title fix(iOS): resolve permission dialog hang when app goes to background fix(iOS): resolve permission dialog hang when app goes to background. Issue: https://github.com/Baseflow/flutter-geolocator/issues/1518 Mar 12, 2026
@guide-linhnv
Copy link
Author

Summary

This PR fixes issue #1518 where Geolocator.requestPermission() Future hangs indefinitely when the iOS app goes to background while the location permission dialog is
displayed.

Problem

On iOS, when the location permission dialog is showing and the user:

  • Locks their phone
  • Switches to another app
  • Triggers any action that backgrounds the app

iOS automatically dismisses the permission dialog without calling the CLLocationManagerDelegate callback. This causes:

  1. The requestPermission() Future to never complete (hang forever)
  2. Subsequent permission requests to fail with: "A request for location permissions is already running"

Root Cause

iOS's CLLocationManager delegate method locationManager:didChangeAuthorizationStatus: is not called when the permission dialog is dismissed due to app backgrounding.
The Flutter side keeps waiting for a response that will never arrive.

Solution

Added app lifecycle monitoring to detect when the app returns to foreground after being backgrounded during a permission request:

  1. Register for UIApplicationDidBecomeActiveNotification when starting a permission request
  2. Track permission request state with isWaitingForPermission flag
  3. On app becoming active:
    • Wait 0.3s to allow any pending system callbacks to process
    • Check if still waiting for permission response
    • Resolve the callback with current authorization status
  4. Proper cleanup of notification observers in both cleanUp and dealloc

Changes

  • PermissionHandler.m: Added lifecycle observer and handling logic (iOS only, wrapped in #if TARGET_OS_IOS)

Testing

  • Tested on iOS 17+ simulator and device
  • Verified permission dialog works normally when user makes a choice
  • Verified Future resolves correctly when app is backgrounded during dialog
  • Verified subsequent permission requests work after backgrounding
  • Verified no impact on macOS (changes are iOS-specific)

Breaking Changes

None. This is a bug fix that doesn't change any public API.

@guide-linhnv guide-linhnv changed the title fix(iOS): resolve permission dialog hang when app goes to background. Issue: https://github.com/Baseflow/flutter-geolocator/issues/1518 fix(iOS): resolve permission dialog hang when app goes to background. Mar 12, 2026
@guide-linhnv
Copy link
Author

Hi @mvanbeusekom,
Could you please help me review this PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant