CheckIfLocationServicesIsEnabled (Purple Warning) iOS 17.0

143 Views Asked by At

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?

0

There are 0 best solutions below