Silver Challenge - working, but a question


#1

Here is what I did for the Silver Challenge in chapter 4. I thought I that the best place to check to see if the device supported headings was in the init method, so I called the location manager’s “headingAvailable” method. There is a depricated property in older versions of OS that held a BOOL value of either YES or NO to tell you if the device supported headings, but that has been replaced with a class method called the same name. My XCode warns me that I’m calling a depricated property, and I don’t know how to fix that. But, I know the method is working because the device does start updating headings.

Next, I use the “startUpdatingHeading” method to tell the device to update the headings when available. If heading information is not available, the method is never reached. Next, I implemented the optional “didUpdateHeading” method and just tell it to give me an update when there is a new heading.

Here is the output from my iPhone 4S when tested on the device: “May 28 09:02:22 unknown Whereami[2188] : The new heading is magneticHeading 131.06 trueHeading 132.44 accuracy 10.00 x -15.028 y -14.350 z -48.395 @ 2012-05-28 14:02:22 +0000.” There are HUNDREDS of lines on the phone’s log for every little change in heading.

Am I doing something wrong in terms of the “headingAvailable” method?


#2

Per the documentation,

So change your myHeading BOOL to

Whenever there is a deprecated method, the documentation will list it at the bottom in the Deprecated Methods section, and will let you know what to use instead, as quoted above.


#3

In the silver challenge we need to find the heading direction. By reading on the manual, it says that in the case of a kCLErrorHeadingFailure we should: “you might want to stop updates for a short period of time and try again later”. How can I do this? By the way, my code for WhereamiViewController.m ends like this:

[code]#import “WhereamiViewController.h”

@implementation WhereamiViewController

  • (id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

      //NSLog(@"Initialising location program");
      
      locationManager = [[CLLocationManager alloc] init];
      
      //[self doSomethingWeird];
      
      [locationManager setDelegate:self];
      
      [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
      
      [locationManager startUpdatingLocation];
      
      [locationManager setDistanceFilter:50.0];   //Bronze challenge
      
      if ([CLLocationManager headingAvailable] == NO) {
          
          NSLog(@"No Compass. This device does not have the ability to measure magnetic fields.");
      }else{
          
          locationManager.headingFilter = 5;
          
          [locationManager startUpdatingHeading];
      }
      
      //NSLog(@"Location program running");
    

    }

    return self;
    }

  • (void) locationManager:(CLLocationManager *) manager
    didUpdateToLocation:(CLLocation *) newLocation
    fromLocation:(CLLocation *) oldLocation {

    NSLog(@"%@", newLocation);
    }

  • (void) locationManager:(CLLocationManager *) manager
    didFailWithError:(NSError *)error{

    if ([error code] == kCLErrorDenied) {
    // This error indicates that the user has denied the application’s request to use location services.
    [manager stopUpdatingHeading];
    [manager stopUpdatingLocation];
    } else if ([error code] == kCLErrorHeadingFailure) {
    // This error indicates that the heading could not be determined, most likely because of strong magnetic interference.
    NSLog(@“Could not find heading direction: %@”, error);
    } else if ([error code] == kCLErrorLocationUnknown) {
    // This error indicates that the heading could not be determined, most likely because of strong magnetic interference.
    NSLog(@“Could not find location: %@”, error);
    }
    }

-(void) locationManager:(CLLocationManager *) manager didUpdateHeading:(CLHeading *)newHeading{
NSLog(@"%@", newHeading);
}

/*

  • (void) doSomethingWeird{
    NSLog(@“Line 1”);
    NSLog(@“Line 2”);
    NSLog(@“Line 3”);
    }*/

  • (void) dealloc{
    [locationManager setDelegate:nil];
    }

@end[/code]