Why the CLLocationManager delegate is not getting called in iPhone SDK 4.0?
This is the code that I have in my AppDelegate Class
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
locationMan开发者_如何学Goager.delegate = self;
locationManager.distanceFilter = 1000; // 1 Km
locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
[locationManager startUpdatingLocation];
}
And this is the delegate method i have in my AppDelegate Class
//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);
}
Its working in 3.0, 3.1, 3.1.3 , but its not working in 4.0 simulator and device both.
What is the reason ?
I had a similar error, and is solved now. The issue is declaring locationManager variable locally. Declare it instead as a class variable and make sure it is retained either directly or via property retain (or strong if you use ARC). That solves the problem! The issue was de locationManager variable was released and never updated the delegate. Here the code:
.h file:
#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 file:
self.locationManager = [[CLLocationManager alloc] init];
_locationManager.delegate = self;
_locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[_locationManager startUpdatingLocation];
I hope it helps.
Does -locationManager:didFailWithError:
in your delegate ever get called? I'm just thinking, maybe you denied access to location data at some point and now don't get prompted, but access is denied.
I was going crazy trying to figure out why just one of several classes that use a CLLocationManager could never get a call to any of the CLLocationManagerDelegate methods.
It turns out: that class was initialized from a non-main thread, so its CLLocationManager was created on a non-main thread... it could even have been a temporary thread. Anyhow, wrapping the call to initialize my class with the below was all that it took to getting my CLLocationManager delegate methods to be called.
dispatch_sync(dispatch_get_main_queue(),
^{
// create your class that will create a CLLocationManager here.
});
So bottom line: Don't create a CLLocationManager on anything but the main thread, or you'll never get your delegate methods called! I once knew this... just been a long time... sigh. Wasted a day to figure this one out again! Since all the CLLocationManager SO's I read never brought this up, I'm posting it here. Hope it helps someone!
Are you sure you are not deallocating the location manager somewhere? The code you have shows it being allocated in the didFinishLaunching... method but then being forgotten about (though it's still retained and so should still work).
Do you even see that initial popup asking you if it's OK to get the user location?
Put the codes in a viewController "which is pushed to navigationController".
My code is.
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];
}
The problem I think is because of ARC, locationManager is released.
精彩评论