2010-06-17 8 views
12

ist der Code, den ich habe in meinem AppDelegate KlasseWarum wird der CLLocationManager-Delegat nicht im iPhone SDK 4.0 aufgerufen? Dieser

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  

    CLLocationManager *locationManager = [[CLLocationManager alloc] init]; 
    locationManager.delegate = self; 
    locationManager.distanceFilter = 1000; // 1 Km 
    locationManager.desiredAccuracy = kCLLocationAccuracyKilometer; 
    [locationManager startUpdatingLocation]; 
} 

Und das ist die Delegatmethode ich habe in meiner AppDelegate Klasse

//This is the delegate method for CoreLocation 
- (void)locationManager:(CLLocationManager *)manager 
    didUpdateToLocation:(CLLocation *)newLocation 
      fromLocation:(CLLocation *)oldLocation 
{ 

     //printf("AppDelegate latitude %+.6f, longitude %+.6f\n", newLocation.coordinate.latitude, newLocation.coordinate.longitude); 

} 

Seine Arbeit in 3.0, 3.1, 3.1.3, aber es funktioniert nicht in 4.0 Simulator und Gerät beide.

Was ist der Grund?

+0

Dank @Scott Christopherson für einige positive Ansatz ..... aber immer noch haben wir Marionetten geworden und müssen unseren Code jedes Mal ändern, wenn es eine neue iPhone SDK-Version gibt. Immer finden einige Methoden veraltet oder nicht funktioniert ... – Biranchi

+0

Vielen Dank @Scott Christopherson Bitte abstimmen, um diese Frage zu schließen .... wie niemand dieses Problem lösen kann ... – Biranchi

+0

Mit demselben Problem konfrontiert, sind Sie sicher, es dauert etwa 40s, um den Standort zu aktualisieren? –

Antwort

21

Ich hatte einen ähnlichen Fehler und ist jetzt gelöst. Das Problem ist, locationManager lokal zu deklarieren. Deklarieren Sie es stattdessen als Klassenvariable, und stellen Sie sicher, dass es entweder direkt oder über die Eigenschaft beibehalten beibehalten wird (oder stark, wenn Sie ARC verwenden). Das löst das Problem! Das Problem war, dass die Variable locationManager veröffentlicht wurde und den Delegierten nie aktualisierte. Hier ist der Code:

.h-Datei:

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


@interface MapViewController : UIViewController <MKMapViewDelegate, CLLocationManagerDelegate> { 
@private 
    MKMapView *_mapView; 
    CLLocationManager *_locationManager; 
} 

@property(nonatomic, strong) CLLocationManager *locationManager; 

@end 

.m-Datei:

self.locationManager = [[CLLocationManager alloc] init]; 
_locationManager.delegate = self; 
_locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
[_locationManager startUpdatingLocation]; 

Ich hoffe, es hilft.

0

Sind Sie sicher, dass Sie den Standortmanager nicht freigeben? Der Code, den Sie haben, zeigt an, dass er in der Methode didFinishLaunching ... zugeordnet ist, aber dann vergessen wird (obwohl er immer noch beibehalten wird und so weiter funktionieren sollte).

Sehen Sie das erste Popup-Fenster, in dem Sie gefragt werden, ob der Benutzerstandort in Ordnung ist?

+0

ja versuchte jede mögliche Lösung .... muss ich das iphone SDK neu installieren ??? ? – Biranchi

2

Wird -locationManager:didFailWithError: in Ihrem Delegat jemals aufgerufen? Ich denke nur, vielleicht haben Sie irgendwann den Zugriff auf Standortdaten verweigert und werden jetzt nicht dazu aufgefordert, aber der Zugriff wird verweigert.

0

Setzen Sie die Codes in einen ViewController "welcher an navigationController" gesendet wird.

Mein Code ist.

In SomeViewController.h

#import <MapKit/MapKit.h> 
@interface SomeViewController : UIViewController<CLLocationManagerDelegate>{ 
} 
@property (nonatomic, strong) CLLocationManager *locationManager; 
@end 

In SomeViewController.m

#import "SomeViewController.h" 

@implementation SomeViewController 


-(void)findLocation{ 
    if ([CLLocationManager locationServicesEnabled]) { 
     if (!self.locationManager) { 
      self.locationManager = [[CLLocationManager alloc] init]; 
      self.locationManager.delegate = self; 
      self.locationManager.distanceFilter = kCLDistanceFilterNone; 
      self.locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
      [self.locationManager startUpdatingLocation]; 
     } 
    } 
} 
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{ 
    NSLog(@"didUpdateLocations"); 

    CLLocation *lo = [locations objectAtIndex:0]; 
    NSLog(@"latitude = %f, longitude = %f", lo.coordinate.latitude, lo.coordinate.longitude); 

    [manager stopUpdatingHeading]; 
} 
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{ 
    NSLog(@"didUpdateToLocation");} 
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{ 
    NSLog(@"didFailWithError"); 
    [manager stopUpdatingLocation]; 
} 

Das Problem, das ich denke, ist wegen der ARC wird Locationmanager freigegeben.

Verwandte Themen