Repository navigation
Open
Labels
P2Important issues not at the top of the work list.platform: iosIssue is related to the iOS platformrefactorIssues marked with refactor should be considered when refactoring the plugin.type: bugSomething isn't working
Description
Please check the following before submitting a new issue.
- I have searched the existing issues.
- I have carefully read the documentation and verified I have added the required platform specific configuration.
Please select affected platform(s)
- Android
- iOS
- Windows
Steps to reproduce
- Check result of requesting locationWhenInUse permissions
await Permission.locationWhenInUse.request(). - Check result of requesting locationAlways permissions
await Permission.locationAlways.request().
Expected results
We expect the both requests to properly await the user's response to the iOS system dialog, and returns the user's choice.
Actual results
In the above steps, the first request properly awaits the user's response to the iOS system dialog, and returns the user's choice.
The second request immediately returns a response of not granted because the user hasn't responded yet to the dialog.
Code sample
Future<bool> _requestLocationWhileInUse() async {
// This actually waits for user to respond to dialog prompt before
// continuing to return the status.isGranted.
final status = await Permission.locationWhenInUse.request();
return status.isGranted;
}
Future<bool> _requestLocationAlways() async {
// This does not wait for user to respond to dialog prompt before
// continuing to return the status.isGranted, thus, we often receive
// false here, and have to check after the user responds to the dialog.
final status = await Permission.locationAlways.request();
return status.isGranted;
}Screenshots or video
Screenshots or video demonstration
[Upload media here]
Version
10.4.3 - 11.0.0
Flutter Doctor output
Doctor output
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.13.0, on macOS 13.5.1 22G90 darwin-arm64, locale en-US)
[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.2)
[✓] Xcode - develop for iOS and macOS (Xcode 14.3.1)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2022.3)
[✓] VS Code (version 1.81.1)
[✓] Connected device (5 available)
[✓] Network resources
• No issues found!Reacted by Agon Noga, Krzysztof Mamak, Tobias Work, Mark Szente, Albert Wolszon, Paweł Jakubowski, GowsikRaja, Jérémie, Michal Hazdra and Rory Stephenson
Metadata
Metadata
Assignees
Labels
P2Important issues not at the top of the work list.platform: iosIssue is related to the iOS platformrefactorIssues marked with refactor should be considered when refactoring the plugin.type: bugSomething isn't working
Activity
jeffscaturro-aka commentedon Sep 13, 2023
Looks like a similar issue was previously filed around this, but marked as resolved: #1086 (=> https://github.com/Baseflow/flutter-permission-handler/pull/1089/files#r1254302197).
niksen75 commentedon Sep 18, 2023
Same issue.
iOS 16.6.1, physical device
Pubspec.yaml
permission_handler: ^10.4.5
permission_handler_apple: ^9.1.4
Podfile
config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= [
'$(inherited)',
## dart: [PermissionGroup.location, PermissionGroup.locationAlways, PermissionGroup.locationWhenInUse]
'PERMISSION_LOCATION=1',
]
mvanbeusekom commentedon Sep 21, 2023
Hi @jeffscaturro-aka, @niksen75,
Thank you for reporting this issue and providing detailed descriptions. I was able to reproduce the problem and did more detailed investigation.
Unfortunately the issue is not really easy to solve in our current architecture where we simply await the future when requesting permissions. The reason is that iOS will not always provide a status update when requesting "Always allow" permissions. Here are the situations:
User selected "Allow once"
When requesting location permissions and the user initially selected "Only once", requesting
locationAlwayswill not trigger the permission dialog at all and no result is provided. Unfortunately Apple doesn't provide an API which informs us the user initially selected "Only once", instead the "When in use" permission is returned. This is also explained in Apple's documentation.User selected "When in use" and follows up with "Keep When in use"
When the user selected "When in use" at the moment we initially request location permissions Apple will return the "When in use" permission. Now if the app requests location always permission the following happens:
locationManager:didChangeAuthorizationStatus:callback with thekCLAuthorizationStatusAuthorizedWhenInUsestatus (this happens before the second permission dialog is shown).User selected "When in use" and follows up with "Change to Allow always"
Again the user initially provided "When in use" permissions and the app is requesting location always permission:
locationManager:didChangeAuthorizationStatus:callback and confirms the current permission reporting thekCLAuthorizationStatusAuthorizedWhenInUsestatus.locationManager:didChangeAuthorizationStatus:method and inform us of the newkCLAuthorizationStatusAuthorizedAlwaysstatus.As you can see this makes it really hard (not to say impossible) to reliably await for the correct status when requesting location alway permission. This due to the fact that in several situations iOS will simply not inform us about the status when the user makes a selection.
We understand that the current solution provided by the plugin is not really helpful and therefore we are planning to do a refactor of the plugin where we will provide developers with the option to listen to a stream providing permission status changes instead of enforcing developers to await the
requestmethod for a status update. We will start the refactor very soon and hopefully can provide updates in the coming months. Until this time, we are not really able to provide a more solid solution and we hope you all understand.We will keep this issue open to help track the refactor process and ensure this issue will be resolved as part of the refactor process.
P.S. alternative ideas are of course welcome.
jeffscaturro-aka commentedon Oct 6, 2023
@mvanbeusekom thank you so much for the triage and in depth explanation here, it is much appreciated and gives a great understanding of the limitations we're facing. I feel most developers can understand Apple doesn't always make it easy on us, especially when factoring in user privacy and permissions (rightfully so).
A workaround we had in place was simply utilizing a WidgetsBindingObserver where we were asking for this permission and listening to its lifecycle state changes (didChangeAppLifecycleState). I hope that helps some others who stumble across this issue, and of course, other ways around this would be welcomed to include on this thread!
Biowulf21 commentedon Feb 21, 2024
For anyone reading this in the future, this is the didChangeAppLifecycleState workaround that people are mentioning:
nayanaubie commentedon Mar 22, 2024
@Biowulf21 What is the exact workaround and what will your method
handleIOSLocationWorkArounddo?I am just wondering as there is no connection between
AppLifecycleStateand this issue.Biowulf21 commentedon Mar 22, 2024
The app goes to suspend state when the location permission pop up appears and goes to resumed state when it's closed.
iJack93 commentedon Nov 12, 2024
Hi @mvanbeusekom,
Is there any news about this permission status stream implementation that you've mentioned above?
Thank you very much!
alle-iacob commentedon Oct 26, 2026
Hi. Is there a timeline scheduled for this fix?
khb-works commentedon Oct 28, 2026
I fixed the locationAlways status in #1577 and looked into this one (and #1457) next. I used the apple example on an iOS 26.5 simulator with current main (0ea2555, permission_handler_apple 9.6.4), and logged when request() returns and when the app lifecycle changes.
The first two rows come from the callback you described in 2023. Right after
requestAlwaysAuthorization, the delegate reports the current When In Use status, and the strategy answers the request with denied inlocationManager:didChangeAuthorizationStatus:. It already observesUIApplicationDidBecomeActiveNotificationfor Always requests, but by the time that fires, the request has already been answered. In the last row nothing answers the request, so_methodResultinPermissionHandlerPluginis never cleared.The third row is why that callback can't just be ignored. After Allow Once, Core Location "ignores further calls to requestAlwaysAuthorization() due to the temporary authorization", so that callback is the only thing that comes back.
As an alternative to the stream refactor, I'd like to end location requests based on the app lifecycle:
requestAlwaysAuthorizationis called, so after Allow Once, later Always requests would return permanentlyDenied without asking.I tried this in a prototype on the same simulator:
Downsides I know of:
This stays inside
LocationPermissionStrategyand doesn't change the API. In the PR I'd add XCTests with an injectedCLLocationManager.#1511 handles the lock screen case for all permissions by failing the pending request with REQUEST_INTERRUPTED when the app goes to the background. For location, this approach returns the actual status instead of an error.
Would you take a PR along these lines?