未调用CLLocationManager委托

时间:2011-10-02 23:51:18

标签: objective-c ios cocoa-touch cllocationmanager

我在设备上使用CLLocationManager并且没有调用委托方法。这是我的代码(我可以看到该类也没有被释放):

#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>

@interface CurrentLocation : NSObject <CLLocationManagerDelegate> {
    CLLocationManager *locationManager;

}
@property (nonatomic, retain) CLLocationManager *locationManager;  

@end

#import "CurrentLocation.h"
#import "SBJson.h"
@implementation CurrentLocation
@synthesize locationManager;

- (id)init
{
    self = [super init];
    if (self) {
        NSLog(@"Initialized");
        locationManager = [[[CLLocationManager alloc] init] autorelease];
        locationManager.delegate = self;
        [locationManager startUpdatingLocation];
        BOOL enable = [CLLocationManager locationServicesEnabled];
        NSLog(@"%@", enable? @"Enabled" : @"Not Enabled");
    }

    return self;
}

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation {
    NSLog(@"A");
    NSString *stringURL = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json?latlng=%f,%f&sensor=true", newLocation.coordinate.latitude, newLocation.coordinate.longitude];
    NSLog(@"%@", stringURL);

    NSURL *url = [NSURL URLWithString:stringURL];

    NSData *data = [NSURLConnection sendSynchronousRequest:[NSURLRequest requestWithURL:url] returningResponse:nil error:nil];

    SBJsonParser *parser = [[SBJsonParser alloc] init];
    NSDictionary *dict = [parser objectWithData:data];
    NSLog(@"%@", dict);
}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    NSLog(@"B");
}

- (void)dealloc {
    NSLog(@"Dealloc called");
    [super dealloc];
    [locationManager release];
}

@end

在日志中,我看到:

2011-10-02 19:49:04.095 DixieMatTracker[1404:707] Initialized
2011-10-02 19:49:04.109 DixieMatTracker[1404:707] Enabled

我做错了什么? 感谢

3 个答案:

答案 0 :(得分:6)

你使用的是locationManager ivar,而不是财产;这意味着CLLocationManager未被保留,并且在您自动释放它时将被取消分配。使用self.locationManager

self.locationManager = [[[CLLocationManager alloc] init] autorelease];

答案 1 :(得分:2)

我建议您检查CLLocationManager实例上的内存管理。

写下来:

#import "CurrentLocation.h"
#import "SBJson.h"
@implementation CurrentLocation
@synthesize locationManager;

- (id)init
{
    self = [super init];
    if (self) {
        NSLog(@"Initialized");
        locationManager = [[CLLocationManager alloc] init];
        locationManager.delegate = self;
        [locationManager startUpdatingLocation];
        BOOL enable = [CLLocationManager locationServicesEnabled];
        NSLog(@"%@", enable? @"Enabled" : @"Not Enabled");
    }

    return self;
}

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation {
    NSLog(@"A");
    NSString *stringURL = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json?latlng=%f,%f&sensor=true", newLocation.coordinate.latitude, newLocation.coordinate.longitude];
    NSLog(@"%@", stringURL);

    NSURL *url = [NSURL URLWithString:stringURL];

    NSData *data = [NSURLConnection sendSynchronousRequest:[NSURLRequest requestWithURL:url] returningResponse:nil error:nil];

    SBJsonParser *parser = [[SBJsonParser alloc] init];
    NSDictionary *dict = [parser objectWithData:data];
    NSLog(@"%@", dict);
}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    NSLog(@"B");
}

- (void)dealloc {
    NSLog(@"Dealloc called");
    [super dealloc];
    [locationManager release];
}

@end

委托不会被调用,因为CLLocationManager对象在它可以提供对位置请求的响应之前被释放。 这是由代码中的自动释放引起的。

除此之外 - 如果使用自动释放,则在dealloc中手动释放它将是错误的。这可能会导致意外崩溃。

上面的解决方案是“手动”在init中分配CLLocationManager并在dealloc中释放它。

答案 2 :(得分:0)

在我的情况下,这是因为我的设备无法确定位置 - 而在室内GPS信号不可用,Wi-Fi也断开连接,因此位置管理员只是无法接收任何位置数据,代表是从未打电话过

一旦我连接了Wi-Fi,它就可以工作了(我想在户外移动也应该可以做到这一点,但有时它不是很方便:))