Solution to Bronze and Silver Challenges


#1

Hi All,

So, I actually implemented some extra code for the silver challenge to check if the H/W the app was on, supports compass data

Here are the files

WhereAmIViewController.h

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>

@interface WhereamiViewController : UIViewController <CLLocationManagerDelegate> //Conforms to the CLLOcationManager protocol 

{
    CLLocationManager *locationManager; 
}

@end

WhereAmIViewController.m

#import "WhereamiViewController.h"

@interface WhereamiViewController ()

@end

@implementation WhereamiViewController


- (id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
        //Create a location manager object

    locationManager = [[CLLocationManager alloc]init];


        //There will be a warning message from this line of code - ignore it for now.

    [locationManager setDelegate:self];
    
        //We want it to be as accurate as possible - regardles of power/time useage

    [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];

        //__Bronze Challenge____________
    
    [locationManager setDistanceFilter:50];

        ///__Silver Challenge______________

        //Check to see if H/W supports heading data
    
    if ([CLLocationManager headingAvailable] == YES)
        {
        [locationManager startUpdatingHeading];
        }else
            {
            NSLog (@ "No Compass found!!" );
            }


        //_______________
    
        //Tell the manager to start looking for its location now
    
    [locationManager startUpdatingLocation];

    return self;
}
    //Update delegate with new location

- (void) locationManager: (CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation: (CLLocation *)oldLocation
{
    NSLog (@ "%@", newLocation); 
}

    //__Silver challenge_____

    // Update delegate with new heading

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

    ///_________

    //Update delegare should there be an error

- (void) locationManager: (CLLocationManager *)manager
didFailWithError: (NSError *)error
{
    NSLog ( @ "Failed to update location: %@", error); 
}


- (void) dealloc
{
        //Tell the locationmanager to stop sending us messages

    [locationManager setDelegate:nil];
}
@end

After reading the Apple documentation - I found that the method used for checking if H/W has support for compass was deprecated in iOS 6.0 - so, I found the replacement, which is a class method.

Tested this on the device and works 100%


#2

So I have the exact same code, except trying on my iOS7 phone. It’s not printing the NSLog heading…