Skip to content
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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

hactar
Copy link

@hactar hactar commented Jan 15, 2024

I've tried using SwiftLocation in a new app I'm creating using async await. I did it according to the readme:

try await location.requestPermission(.whenInUse) // obtain the permissions
let userLocation = try await location.requestLocation() // get the location

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 the await 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.

@malcommac
Copy link
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?

@hactar
Copy link
Author

hactar commented Mar 5, 2024

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)
                }
            }
        }

@bcbeta
Copy link

bcbeta commented Sep 29, 2024

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.

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.

3 participants