I am trying to get iphone compass to work using rhomobile framework. I already made the rhomobile part, created a working shell that calls native methods on the iphone, but I cannot get the events to work.
Locationmanager.h
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
@interface locationController : NSObject <CLLocationManagerDelegate>
{
CLLocationManager *locationManager;
}
@property (strong, nonatomic) CLLocationManager *locationManager;
- (id)init;
- (void)dealloc;
@end
Locationmanager.m
#import "Locationmanager.h"
#include "ruby/ext/rho/rhoruby.h"
double gx, gy, gz, gth;
locationController *lc;
@implementation locationController
@synthesize locationManager;
- (id)init {
if (self = [super init]) {
self.locationManager = [[CLLocationManager alloc] init];
NSLog(@"%@", [CLLocationManager headingAvailable]? @"\n\nHeading available!\n" : @"\n\nNo heading..\n");
NSLog(@"%@", [CLLocationManager locationServicesEnabled]? @"\n\nLocation available!\n" : @"\n\nNo location..\n");
if ([CLLocationManager headingAvailable] == NO) {
locationManager = nil;
UIAlertView *noCompassAlert = [[UIAlertView alloc] initWithTitle:@"No Compass!" message:@"This device does not have the ability to measure magnetic fields." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[noCompassAlert show];
[noCompassAlert release];
NSLog(@"\n***** ERROR *****\n No compass found !!!");
} else {
locationManager.delegate = self;
locationManager.headingFilter = kCLHeadingFilterNone;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
[locationManager startUpdatingLocation];
[locationManager startUpdatingHeading];
}
return self;
}
}
- (void)dealloc {
[super dealloc];
[locationManager stopUpdatingHeading];
[locationManager release];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)heading {
NSLog(@"\n\n***** New magnetic heading *****\n %f\n", heading.magneticHeading);
NSLog(@"\n\n***** New true heading *****\n %f\n", heading.trueHeading);
gx = heading.x;
gy = heading.y;
gz = heading.z;
gth = heading.trueHeading;
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
if ([error code] == kCLErrorDenied) {
NSLog(@"\n ***** ERROR *****\n Location not allowed!");
[locationManager stopUpdatingHeading];
} else if ([error code] == kCLErrorHeadingFailure) {
NSLog(@"\n ***** ERROR *****\n Magnetic interference or something!");
}
}
@end
void locationmanager_init(void) {
static bool started = false;
if(!started) {
lc = [[locationController alloc] init];
started = true;
}
}
void locationmanager_get_heading(double *x, double *y, double *z, double *th) {
NSLog(@"\n ***** DEBUGGER *****\n Getting heading x: %f, y: %f, z: %f, heading: %f", gx, gy, gz, gth);
*x = gx;
*y = gy;
*z = gz;
*th = gth;
}
I run the code on iphone 4 with iOS 5.1, in the console I can see init debug messages, but I never see the didUpdateHeading delegate debug message. Does anyone understand what I missed here?
UPDATE
, , . locationmanager_init +, , .
- , , ?
2
id, self = [super init] - : (
GitHub
locationmanager_init, locationmanager_get_heading