With the release of iOS6 Apple wants us to use didUpdateLocations instead of didUpdateToLocation. Can anyone explain how to properly use didUpdateLocations?
相关问题
- CALayer - backgroundColor flipped?
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- State preservation and restoration strategies with
- “Zero out” sensitive String data in Swift
相关文章
- 现在使用swift开发ios应用好还是swift?
- UITableView dragging distance with UIRefreshContro
- Using if let syntax in switch statement
- TCC __TCCAccessRequest_block_invoke
- Where does a host app handle NSExtensionContext#co
- xcode 4 garbage collection removed?
- Unable to process app at this time due to a genera
- Enum with associated value conforming to CaseItera
If you support both iOS 5 and 6, you should call
from the older
function, by building an array of locations.
It gives you array of objects to access last location you can use
from this delegate method
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 usingallowDeferredLocationUpdatesUntilTraveled:timeout:
method. Some conditions apply, see this answer for details.This is how the method can be implemented to work similarly as the deprecated one
I asume you used the following delegate to get the last position?
The delegate above is deprecated in iOS 6. Now the following should be used:
In order to get the last position, simply get the last object of the array:
In other words,
[locations lastObject]
(new delegate) equalsnewLocation
(old delegate) .It is noteworthy that the array of CLLocation objects returned by locationManager:didUpdateLocations: may or may not include a former location. In other words, there may be on rare occasions only one location in the array. Use the following to check, and if there is more than one object we can retrieve the most recent prior location:
As there will be at least one object in the array, retrieving the current location is done by requesting the last object in the array. Even if the last object is the only object, that's Ok:
Keep in mind that because this is an array, oldLocation in the example shown above will not necessarily be the one you're looking for. This depends on how you setDistanceFilter: and desiredAccuracy: properties, as those properties will impact how your locations array is populated. The desired oldLocation may be buried deeper in the array.