OK the code is now working, but it still needs work. The values I get are “sticky”, they are not stable (the magnetic North seems to move a bit every time I try to return to it), and I need to shake the device a bit in order to refresh/wake-up the values..
Game.h
#import <Foundation/Foundation.h>
#import "CoreLocation.h"
@interface Game : NSObject
<CLLocationManagerDelegate>
@property BOOL stopButtonPressed;
-(void) play;
@end
Game.m
@implementation Game
- (id) init
{
self = [super init];
self.stopButtonPressed = NO;
CLLocationManager *locationManager;
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
return self;
}
-(void) play
{
[locationManager startUpdatingHeading];
while(!self.stopButtonPressed)
{
double degrees = locationManager.heading.magneticHeading;
int degreesRounded = (int)degrees;
NSLog(@"Degrees : %i", degreesRounded);
}
}
@end
MyViewController.m
@interface MyViewController()
{
Game *game;
}
@end
@implementation MyViewController
-(void) viewDidLoad
{
game = [[Game alloc] init];
}
- (IBAction)playPressed:(UIButton *)sender
{
[game performSelectorInBackground:@selector(play) withObject:nil];
}
- (IBAction)stopPressed:(UIButton *)sender
{
game.stopButtonPressed = YES;
}
@end
What am I doing wrong?
This code will block the thread, and if it’s happening in the main thread, you will never get the button press.
CLLocationManager is an asynchronous mechanism. To work with it properly, you must provide a delegate which it will notify when updates to location are available (this can be
selfin most cases (whereselfis a viewController or similar). See CLLocationManagerDelegate docs