Christopher Rung
Christopher Rung

Reputation: 436

CLLocationManager.location is nil in 10.10, but works in 10.11

In my DC Metro tracking application, I use CoreLocation to select the Metro station nearest the user and also present a list of stations near them.

It works perfectly in macOS 10.11, but I'm having trouble getting it to work on macOS 10.10. To debug this, I inserted a line in the locationManager(_:didUpdateLocations:) method in TodayViewController.swift to print the location fetched by the application.

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [AnyObject]) {
    LocationManager.sharedManager.stopUpdatingLocation()

    print(LocationManager.sharedManager.location)

    ...
}

On El Capitan, this outputs the following to the console (success):

Optional(<+38.92208178,-77.22920176> +/- 65.00m (speed -1.00 mps / course -1.00) @ 8/18/16, 5:04:37 PM Eastern Daylight Time)

On Yosemite, it just outputs nil.

I have also tried to simulate my location to no avail.

Does anyone have any suggestions? Location services are enabled on the Yosemite machine, and I know that it is working because the Weather Notification Center widget is correctly fetching its location.

Thank you!

More relevant code:

override func viewWillAppear() {
    super.viewWillAppear()

    ...

    switch CLLocationManager.authorizationStatus() {
    case .Authorized:
        if !didSelectStation {
            selectedStationLabel.stringValue = "Determining closest station..."
        }
        LocationManager.sharedManager.startUpdatingLocation()
    case .NotDetermined:
        getCurrentLocationButton.hidden = false
        mainPredictionView.hidden = true
    default:    // Denied or Restricted
        WMATAfetcher.getPredictionsForSelectedStation()
    }
}

class LocationManager {
    static let sharedManager: CLLocationManager = {

        let locationManager = CLLocationManager()
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.distanceFilter = 100.0

        return locationManager
    }()
}

I also have a button, getCurrentLocationButton, that calls startUpdatingLocation()

@IBAction func getCurrentLocation(sender: NSButton) {
    LocationManager.sharedManager.startUpdatingLocation()
}

Upvotes: 1

Views: 168

Answers (1)

Deyton
Deyton

Reputation: 2368

Have you tried accessing location before calling stopUpdatingLocation()?

Upvotes: 1

Related Questions