2012-08-31 17 views
5

मैं रममोबाइल ढांचे का उपयोग करके काम करने के लिए आईफोन का कंपास प्राप्त करने की कोशिश कर रहा हूं। मैंने पहले से ही रमोबाइल भाग किया है, एक काम करने वाला रैपर बनाया है जो आईफोन पर देशी तरीकों को कॉल करता है, लेकिन मैं घटनाओं को काम करने के लिए प्रबंधित नहीं कर सकता।CLLocationManager प्रतिनिधि प्रारंभिकरण के बाद काम नहीं कर रहे हैं

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's 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; 
} 

मैं एक iPhone 4 पर कोड चल रहा हूँ आईओएस 5.1 के साथ, कंसोल में मैं init की डिबग संदेश देख सकते हैं, लेकिन मैंने didUpdateHeading प्रतिनिधि का एक डीबग संदेश कभी नहीं देखा। किसी को भी एक सुराग मिला जो मुझे याद आया?

अद्यतन

मुझे लगता है मैं यह काम कर पाने के लिए एक पृष्ठभूमि सूत्र में मेरी कोड चलाने की आवश्यकता है। वर्तमान में locationmanager_init प्रारंभ होता है + कोड छोड़ देता है, क्योंकि यह सक्रिय नहीं है और घटनाओं को निकाल दिया नहीं जाता है। किसी को भी इसे सक्रिय रखने के लिए पृष्ठभूमि में इसे शुरू करने का एक आसान समाधान मिला है?

अद्यतन 2

आईडी वापस लौटाया, self = [super init] और अभी भी कोई ठीक :(

locationmanager_init साथ

GitHub code initializes इस्तेमाल किया,

+0

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

+0

मैं के साथ और में प्रतिनिधियों को परिभाषित करने के बिना इसे करने की कोशिश पर एक नज़र डालें एच फाइल। NSObject के बजाय UIViewController का भी उपयोग किया गया, जैसा कि यह ऐप्पल के टेस्लामीटर उदाहरण में किया गया है, अभी भी कोई प्रगति नहीं है :( – Vikko

+0

मुझे अपना कोड ईमेल करें। – fibnochi

उत्तर

13

आप मुख्य थ्रेड पर CLLocationManager init करने के लिए है, इस जाँच अतः here, या यह एक सक्रिय रन पाश के साथ एक धागे से चलाने के लिए, यह इतना here जाँच, एप्पल प्रलेखन से:

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 इसके अंदर प्रारंभिक हैं, here देखें। मैं यहां गलत हो सकता हूं, लेकिन आपके गीथब कोड से, ऐसा लगता है कि *lc वैरिएबल ऑटोरेलीज पूल में रिलीज़ हो रहा है। इसे अतिरिक्त बनाए रखने का प्रयास करें।

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

मुझे लगता है कि यह आपकी समस्या का कारण है। यदि ऑब्जेक्ट जारी किया गया है तो आपको कोई अपडेट नहीं मिलेगा।

प्रश्न से संबंधित नहीं है, लेकिन:

आप [super dealloc] पिछले नहीं बल्कि पहले बुलाना चाहिए, अतः here

पिछले कोष्ठक से पहले अपने init विधि में वापसी कथन रखो, और दूसरे से पहले नहीं इस जाँच पिछले।

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

     } 
    } 
    return self; 
} 
+0

मैंने कोशिश की और गिटूब पर कोड अपडेट किया। एक्सकोड में कंसोल में मुझे कोई शीर्षक दिखाई नहीं दे रहा है, हालांकि मुझे कुछ त्रुटि मिली है जो मैंने पहले देखा था: '13 सितंबर 13:00 : 44 अज्ञात CommCenter [56] : kDataAttachStatusNotification भेजा गया था, अटैचमेंट: 1 isAttached: 1' – Vikko

+0

'performSelectorOnMainThread' के साथ आपके उत्तर को संयोजित करने के बाद यह काम करता है !! आपकी मदद के लिए धन्यवाद, मैं इस बारे में लगभग 7 सप्ताह के लिए अटक गया हूं:) आप कल्पना कर सकते हैं कि मैं अब कितना खुश हूं कि मैं अपना विकास जारी रख सकता हूं। वर्किंग कोड अपडेट @ github – Vikko

+0

मुझे इसकी दूसरी समस्या भी होने की समस्या थी। मुझे यह सोचने में डर है कि मुझे आपके जवाब के बिना कितना समय लगेगा, तो बहुत बहुत धन्यवाद! – poshaughnessy

0
- (void)locationManager:(CLLocationManager *)manager 
didUpdateHeading:(CLHeading *)heading; 
- (void)locationManager:(CLLocationManager *)manager 
didFailWithError:(NSError *)error; 

locationmanager_get_heading साथ डेटा को पुन: प्राप्त ये आपके उदाहरण तरीके हैं प्रतिनिधि विधियों नहीं।

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

+3

प्रश्न आईओएस के लिए है (जिसने एक अद्यतन किया है प्रतिनिधि प्रतिनिधि विधि)। आपका लिंक ओएस एक्स दस्तावेज को इंगित करता है। – Anna

0

संभवतः इस समग्र समस्या का हल है, लेकिन अपने init विधि में एक वापसी कथन याद आ रही है नहीं है:

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

    return self; 
} 
+0

मैं शुरू करने के कई रूपों के साथ खेल रहा हूं और मैं इसे वापस रखना भूल गया: पी हालांकि यह कुछ हल नहीं करता है, यह वहां होना चाहिए, रिपोर्ट के लिए धन्यवाद! – Vikko

+0

मुझे यह भी लगता है कि समस्या आपके [init] के साथ है। अपने कोड टुकड़ों से, आपको स्पष्ट रूप से *** self = [super init] *** सेट करना होगा, क्योंकि स्वयं अभी तक तैयार नहीं है, जब तक कि आप इसे टिलो सुझाव के अनुसार नहीं करते हैं, अगर (self = [super init]) {.. ।}। सौभाग्य! – dklt

+0

यह कोशिश की और कोई फिक्स नहीं, बहुत बुरा :( – Vikko

संबंधित मुद्दे