-
Notifications
You must be signed in to change notification settings - Fork 430
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CoreLocation returns .notDetermined before Pop Up Button is pressed #380
base: master
Are you sure you want to change the base?
Conversation
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? |
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
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
.notDetermined
when 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 theawait
has 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.