Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

didUpdateLocations instead of didUpdateToLocation

With the release of iOS6 Apple wants us to use didUpdateLocations instead of didUpdateToLocation. Can anyone explain how to properly use didUpdateLocations?

like image 217
YogevSitton Avatar asked Sep 26 '12 13:09

YogevSitton


2 Answers

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) .

like image 101
Anne Avatar answered Oct 14 '22 00:10

Anne


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.

like image 35
progrmr Avatar answered Oct 14 '22 00:10

progrmr