Werem
Werem

Reputation: 584

What's the proper way to use startMonitoringSignificantLocationChanges in swift 4?

I try to make a running app, and I want to use the method startMonitoringSignificantLocationChanges, but I'm missing something because is not firing the didUpdateLocations that the documentation says it should.

I have successfully used the startUpdatingLocation method of the CLLocationManager class and I can totally can use it for my interests, but I'd like to understand why my implementation doesn't work.

So, I have this code:

let locationManager = CLLocationManager()
func determineMyCurrentLocation() {

    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.requestAlwaysAuthorization()
    locationManager.allowsBackgroundLocationUpdates = true

    print("authorization Status: \(CLLocationManager.authorizationStatus().rawValue)")

    if CLLocationManager.locationServicesEnabled() {
        locationManager.startUpdatingLocation() //(1)
        //(2)locationManager.startMonitoringSignificantLocationChanges()
        locationManager.distanceFilter = 20
        //locationManager.start
        //locationManager.startUpdatingHeading)(
    }
}

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { print("yaaay!") }

If you comment (1) and uncomment 2 the func locationManager...etc is never fired.

Also, I have my info.plist with the proper keys set, "Privacy - Location When In Use Usage Description" and "Privacy - Location Always and When In Use Usage Description").

Any idea?

Thanks in advance.

EDITED: My question was imprecise. I put the locationManager property as a class member and I completed the properties in my info.plist

Upvotes: 3

Views: 2154

Answers (1)

Mugunth
Mugunth

Reputation: 14499

The startMonitoringSignificantLocationChanges requires always authorisation. That also means you should turn on "Background Modes" in Capabilities section and selection "Location Updates". You also require the Privacy - Location Always Usage Description Try these two and you should receive significant location change updates.

Upvotes: 2

Related Questions