didUpdateLocations instead of didUpdateToLocation
I asume you used the following delegate to get the last position?
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
The delegate above is deprecated in iOS 6. Now the following should be used:
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations
In order to get the last position, simply get the last object of the array:
[locations lastObject]
In other words, [locations lastObject]
(new delegate) equals newLocation
(old delegate) .
None of the other answers here explain why there is a locations array and how to properly use the new didUpdateLocations: array that is provided.
The purpose of deprecating the locationManager:didUpdateToLocation:fromLocation:
and sending an NSArray of locations instead is to reduce power consumption when running in the background.
Starting with iPhone 5, the GPS chip has the capability to store locations for a period of time and then deliver them all at once in an array. This is called deferred location updates. This allows the main CPU to sleep for a longer period of time while in the background. It means iOS doesn't have to start the main CPU for every position update, the CPU can sleep, while the GPS chip collects locations.
You can check for this capability using the deferredLocationUpdatesAvailable
method. If available you can enable it using allowDeferredLocationUpdatesUntilTraveled:timeout:
method. Some conditions apply, see this answer for details.
It gives you array of objects to access last location you can use
[locations lastObject]
from this delegate method
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations