Fix/ios background permission reset by zenixio · Pull Request #1511 · Baseflow/flutter-permission-handler
Issue
IOS permission request state not being reset when app enters background, causing subsequent requests to fail with ERROR_ALREADY_REQUESTING_PERMISSIONS.
Video : https://github.com/user-attachments/assets/0f78390a-cbd3-4362-87c8-2874ae4ac620
Changes:
- Added background notification observer to reset state in PermissionHandlerPlugin.m
- Properly cleanup observer in dealloc
Reproduction Steps (Before Fix)
- Call
await Permission.location.request() - When iOS system permission dialog appears, block screen
- Return to app
- Call
await Permission.location.request()again - Result: Exception thrown:
ERROR_ALREADY_REQUESTING_PERMISSIONS
Expected Behavior
If the app is backgrounded during a permission request:
- The pending Flutter callback should be completed with an error (
REQUEST_INTERRUPTED) - The internal state should be reset (
_methodResult = nil) - Subsequent permission requests should work normally without throwing
ERROR_ALREADY_REQUESTING_PERMISSIONS - The user should be able to retry the permission request when they return to the app
Actual Behavior (Before Fix)
- The pending Flutter callback is never completed (Future hangs indefinitely)
- The internal state remains set (
_methodResult != nil) - All subsequent permission requests immediately fail with
ERROR_ALREADY_REQUESTING_PERMISSIONS - The app becomes unusable for any permission-related functionality until restarted
Solution
- Register observer for
UIApplicationDidEnterBackgroundNotificationwhen plugin initializes - When app backgrounds with a pending request, complete the Flutter callback with
REQUEST_INTERRUPTEDerror and reset internal state
After Fix
Same reproduction steps now work correctly:
- Call
await Permission.location.request() - Block Screen → App backgrounds
- Return to app
- Call
await Permission.location.request()again - Result: New permission request works normally, dialog appears again
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.yamlwith an appropriate new version according to the pub versioning philosophy, or this PR is does not need version changes. - I updated
CHANGELOG.mdto 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 analyzeand fixed any errors.