CoreLocation returns .notDetermined before Pop Up Button is pressed#380
Open
hactar wants to merge 14 commits intomalcommac:masterfrom
Open
CoreLocation returns .notDetermined before Pop Up Button is pressed#380hactar wants to merge 14 commits intomalcommac:masterfrom
hactar wants to merge 14 commits intomalcommac:masterfrom
Conversation
Owner
|
I've tried to replicate the issue with no luck: @IBAction public func ciao() {
Task {
do {
try await location.requestPermission(.whenInUse) // obtain the permissions
print("requested permission")
let userLocation = try await location.requestLocation() // get the location
print("called immediately")
} catch {
print(error.localizedDescription)
}
}
}In your case "called immediately" is executed right after the system popup is showed, right? |
Author
|
I tried again - I think it depends on where you init Location(). If you do it within the Task the issue I describe above occurs. If I init location as part of a struct variable (on the main thread) this issue does not occur. Maybe initing Location() within a Task isn't valid, but then there should be warnings about this I guess... .onAppear {
Task {
do {
let location = Location()
print("requesting permission")
try await location.requestPermission(.whenInUse) // obtain the permissions
print("requested permission")
let userLocation = try await location.requestLocation() // get the location
print("called immediately")
print("userLocation: \(userLocation)")
} catch {
print(error.localizedDescription)
}
}
} |
|
If you init Location in the main thread you get a warning. Is it safe to init within a Task? Update: not safe. I get some same issue where requestLocation() returns immediately while waiting for authorization. |
fix cancelation before continuation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
I've tried using SwiftLocation in a new app I'm creating using async await. I did it according to the readme:
I was however surprised that even though the app is supposed to await the requestPermission, in practice the app nearly immediately tried to request a location even though no button on the permission popup was tapped. Obviously no location was returned as no permission was granted.
Investigating the issue I found that CoreLocation immediately returns
.notDeterminedwhen the popup appears, at least it does while I was testing it on iOS 17.2. Then when the user taps a button, the correct permission is returned, but by then theawaithas already been continued with the wrong value:.notDetermined.So I have added code to ignore
.notDetermined- this now works for me, only once a user taps a button on the pop up, does the await return.