A quick tutorial today. I was recently asked how to get a timestamp from a CLLocation object, particularly when the -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations method was used in the CLLocationManagerDelegate protocol. As with most programming solutions, there’s always more than one way to accomplish this. Today, I’ll show you one of the ways it can be done.
Assuming you have your CLLocationManagerDelegate all configured correctly and that you also have your CLLocationManager, this is one way you can get the timestamp from the CLLocation update.
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
CLLocation *newLocation = [locations lastObject];
NSLog(@"Timestamp %@", newLocation.timestamp);
}
Line 2 is a follow on from the CLLocationManagerDelegate tutorial from yesterday. On this line we simply create a new CLLocation called newLocation and taking the NSArray called locations from the method, we take the lastObject from it and store it in newLocation.
In the documentation we find that the CLLocation class has a timestamp property that can be accessed. To access this in the code above, we can simply use dot notation and call newLocation.timestamp to get the timestamp of the last location update.
Overall, a relatively simple bit of code. Take a look through the documentation to see what other properties are available. These include accuracy, coordinate, altitude as well as a few more which can come in handy when creating your iOS app.
Leave a Reply
You must be logged in to post a comment.