Silver Challenge: Works in code, not in iPhone


#1

Hi all,

I’ve got the silver challenge for Chapter 4 done, and my code (below) looks like some of the other examples I’ve seen here. When I run it on the iPhone 6.0 simulator, it correctly tells me I have no compass. However, when I load it up onto my actual iPhone, it does nothing at all: I have a white screen on my phone when I run the application (which is what I expect, having done nothing with the UI), but it doesn’t put out anything to the console either.

Some of the other members here have said that they just plugged in their phone and ran the test on that and were able to see location and heading info. What am I missing? I was able to go on and do everything in chapter 5 and the associated challenges fine, so I’m sure this is something simple. Do I just need to go build something in the UI to display the console log? Am I mistaken for thinking it should show up in Xcode when running the test on my phone?

ETA: I’m testing it on an iPhone 4s running 6.1.

Thank you so much!

WhereamiViewController.h

[code]//
// WhereamiViewController.h
// Whereami2
//
// Created by Raven on 12/1/12.
// Copyright © 2012 Serin. All rights reserved.
//

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

@interface WhereamiViewController : UIViewController
{
CLLocationManager *locationManager;
}
@end
[/code]

WhereamiViewController.m

[code]//
// WhereamiViewController.m
// Whereami2
//
// Created by Raven on 12/1/12.
// Copyright © 2012 Serin. All rights reserved.
//

#import “WhereamiViewController.h”

@interface WhereamiViewController ()

@end

@implementation WhereamiViewController

  • (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {
    //Create location manager object
    locationManager = [[CLLocationManager alloc] init];
    [locationManager setDelegate:self];
    [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
    [locationManager startUpdatingLocation];
    //bronze
    [locationManager setDistanceFilter:50];
    //silver
    if ([CLLocationManager headingAvailable]) {
    [locationManager startUpdatingHeading];
    [locationManager setHeadingFilter:5];
    } else {
    NSLog(@“No compass! Cannot update heading.”);
    }
    }
    return self;
    }

  • (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading
    {
    NSLog(@“New heading: %@”, newHeading);
    }

  • (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
    {
    NSLog(@"%@", [locations lastObject]);
    }

  • (void)locationManager:(CLLocation *)manager didFailWithError:(NSError *)error
    {
    NSLog(@“Could not find location: %@”, error);
    }

//override dealloc

  • (void)dealloc
    {
    //tell the locaton manager to stop sending us messages
    [locationManager setDelegate:nil];
    }

  • (void)viewDidLoad
    {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    }

  • (void)didReceiveMemoryWarning
    {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    }

@end
[/code]


#2

I am having the same issue. I noticed your post is a few months old now, were you able to solve this?

–Dave


#3

You could try:

instead of:

It looks like you are asking the class, rather than the instance whether or not heading is available.