CLLocationManager delegates do not work after initialization

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"

//store the values
double gx, gy, gz, gth;

//init location
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");

    // check if the hardware has a compass
    if ([CLLocationManager headingAvailable] == NO) {
        // No compass is available. This application cannot function without a compass, 
        // so a dialog will be displayed and no magnetic data will be measured.
        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 {
        // setup delegate callbacks
        locationManager.delegate = self;

        // heading service configuration
        locationManager.headingFilter = kCLHeadingFilterNone;

        // location service configuration
        locationManager.distanceFilter = kCLDistanceFilterNone;
        locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;

        //start location services
        [locationManager startUpdatingLocation];

        // start the compass
        [locationManager startUpdatingHeading];

    }
return self;
}
}
- (void)dealloc {    
    [super dealloc];
    // Stop the compass
    [locationManager stopUpdatingHeading];
    [locationManager release];
}

// This delegate method is invoked when the location manager has heading data.
- (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;
}

// This delegate method is invoked when the location managed encounters an error condition.
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    if ([error code] == kCLErrorDenied) {
        // This error indicates that the user has denied the application request to use location services.
        NSLog(@"\n ***** ERROR *****\n Location not allowed!");
        [locationManager stopUpdatingHeading];
    } else if ([error code] == kCLErrorHeadingFailure) {
        NSLog(@"\n ***** ERROR *****\n Magnetic interference or something!");
    }
}

@end

//ruby wrappers
void locationmanager_init(void) {
   // make sure we can only start this method once
   static bool started = false;
   if(!started) {
       // Initialize the Objective C accelerometer class.
       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

+5
3

CLLocationManager , SO , SO , Apple:

Configuration of your location manager object must always occur on a thread with 
an active run loop, such as your application’s main thread.

, locationController CCLocationManager , . , , Github , *lc . .

lc = [[[locationController alloc] init] retain];

, . , .

, :

[super dealloc] , , SO

return init , .

        ...
        // start the compass
        [locationManager startUpdatingHeading];

        }
    }
    return self;
}
+13
- (void)locationManager:(CLLocationManager *)manager 
didUpdateHeading:(CLHeading *)heading;
- (void)locationManager:(CLLocationManager *)manager 
didFailWithError:(NSError *)error;

.

https://developer.apple.com/library/mac/#documentation/CoreLocation/Reference/CLLocationManagerDelegate_Protocol/CLLocationManagerDelegate/CLLocationManagerDelegate.html

0

Perhaps this is not a solution to the general problem, but there is initno return statement in your method :

- (id)init {
  if (self = [super init]) {
    // do your setup here
  }

  return self;
}
0
source

All Articles