Gerard Wilkinson
Gerard Wilkinson

Reputation: 1541

CLLocationManager didUpdateLocations not being called iOS 8 with plist entries

I'm having some issues with CLLocationManager. This code used to work on iOS 8.2 but since upgrading to 8.3 it doesn't work. Here is the code for setting up the location manager which is called on startup.

let distanceThreshold:CLLocationDistance = 100.0

var currentLocation:CLLocationCoordinate2D?

override init() {
    assert(locMan == nil)
    super.init()
    locMan = self

    let locationManager = CLLocationManager()
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters
    locationManager.requestAlwaysAuthorization()
    locationManager.requestWhenInUseAuthorization()
    locationManager.startUpdatingLocation()
}

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
    currentLocation = manager.location.coordinate

    if UIApplication.sharedApplication().applicationState == .Background {
        PlacesManager.fetchNearbyPlaces(LocationManager.getLocationManager().currentLocation!, radius: distanceThreshold, callback: placesCallback)
    }
}

With this code didUpdateLocations is never called despite it being called before. I have added the relevant entries to the Info.plist file: Info.plist screenshot

I have tried it on both a device and the simulator and neither works. In fact it seems that it is no longer requesting location authorisation anymore if I delete the app and reinstall.

I know I'm missing something stupid but I can't workout what the hell it is.

I'd appreciate any help people can provide.

Cheers, Gerard

Upvotes: 2

Views: 3345

Answers (2)

Chase Roberts
Chase Roberts

Reputation: 9386

In addition to the accepted answer, if you are using the simulator. You have to select a location after the app is running (Debug->Location->apple for apple headquarters). I set the location and assumed that the next time I ran the app that didUpdateLocations would be called with what I had set previously, but that assumption was wrong.

Upvotes: 1

Gerard Wilkinson
Gerard Wilkinson

Reputation: 1541

Answer from Anna:

The CLLocationManager documentation says: "To configure and use a CLLocationManager object to deliver events...Create an instance of the CLLocationManager class and store a strong reference to it somewhere in your app. Keeping a strong reference to the location manager object is required until all tasks involving that object are complete. Because most location manager tasks run asynchronously, storing your location manager in a local variable is insufficient."

Upvotes: 4

Related Questions