I’m working through Big Nerd Ranch’s iOS Programming, 2nd Edition, and I’ve come to the Chapter 4 Challenge: Heading. The exercise offers a hint that I’m finding confusing; it says I need to do things that I don’t think I need to do.
Up to this point in the chapter, the book has walked me through the concept of delegation, using CoreLocation as an example. In the app delegate, I’ve added a CLLocationManager instance variable and set its delegate to be the app delegate. See below:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
locationManager = [[CLLocationManager alloc] init];
[locationManager setDelegate:self];
[locationManager setDistanceFilter:kCLDistanceFilterNone];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[locationManager startUpdatingLocation];
[self.window makeKeyAndVisible];
return YES;
}
I’ve also implemented the two delegate methods for CLLocationManager, as follows:
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
NSLog(@"%@", newLocation);
}
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error
{
NSLog(@"Could not find location: %@", error);
}
Ok, so the chapter has walked me through all of this so far, and everything works nicely. Now comes the exercise:
“Using delegation, retrieve the heading information from the
CLLocationManager and print it to the console. (Hint: You need to implement at least one delegate method and send another message to the
location manager.)”
Here’s where I’m confused. First of all, after reading the CLLocationManager documentation, it appears as though there are no delegate methods to be implemented other than the two I’ve already done. So I don’t know what other delegate method the hint could be referring to.
Second of all, it seems as though I could solve the exercise simply by updating the implementation of locationManager:didUpdateToLocation:fromLocation: like so:
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
NSLog(@"%@", newLocation);
// Updated code below
CLLocationDirection heading = [newLocation course];
NSLog(@"Heading: %f", heading);
}
So it seems to me as though I don’t, in fact, need to send any more messages to the location manager (although I do need to send a getter message to newLocation; but that’s a CLLocation, not a CLLocationManager).
So I’m clearly misunderstanding something. I’m not looking for a solution—I do want to work this through—but I would very much appreciate a hint as to what I’m misunderstanding. Thanks in advance!
I think they are asking you to get the compass (magnetic) heading.
Look back at the CLLocationManager docs again. There are -startUpdatingHeading and -stopUpdatingHeading methods and corresponding delegate methods to get the compass heading from the device.
Hope that helps.