0

I am trying to remove a Purple Warning against CheckIfLocationServicesIsEnabled/.locationServicesEnabled when trying to resolve the user's location.

This is based on Sean Allen's YouTube video "How to Get User Location on Map - Swift" which is a year old and written around Xcode 13/iOS 15.0 (beta). The code below works and takes the Map to the user's location. BUT a purple warning appears...

final class LocationViewModel: NSObject, ObservableObject, CLLocationManagerDelegate {

var locationManager:  CLLocationManager?
@Published var region = MKCoordinateRegion(center: MapDetails.startingLocation, span: MapDetails.defaultSpan)

func checkIfLocationServicesIsEnabled() {
    DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
        // DispatchQueue.global().asyncAfter(deadline: .now() + 2) {
        //  DispatchQueue.global().async {
        if CLLocationManager.locationServicesEnabled() {
            self.locationManager = CLLocationManager()
            self.locationManager!.delegate = self
            self.locationManager?.desiredAccuracy = kCLLocationAccuracyBest
            // self.locationManager?.startUpdatingLocation()
        } else {
            print("Location services are not enabled, please turn on.")
        }
    }
}
...
}

against the line:

if CLLocationManager.locationServicesEnabled() {

with "This method can cause UI unresponsiveness if invoked on the main thread. Instead, consider waiting for the -locationManagerDidChangeAuthorization: callback and checking authorizationStatus first."

I have been trying with a DispatchQueue to resolve this. If I use either of the DisaptchQueue.global() approaches I don't get the purple warning but the map does update from Apple's HQ to the user's Location.

// DispatchQueue.global().asyncAfter(deadline: .now() + 2) { or 
// DispatchQueue.global().async {

Any ideas?

Edward Hasted
  • 3,201
  • 8
  • 30
  • 48
  • `locationServicesEnabled` has been giving that warning since iOS 15, I think. It's certainly not new to iOS 17. You don't need to call it. Just handle the state in your `locationManagerDidChangeAuthorization` delegate method. – HangarRash Aug 09 '23 at 07:05
  • See my answer here: https://stackoverflow.com/a/74915706/20287183 – HangarRash Aug 09 '23 at 07:08

0 Answers0