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
我做错了什么? 谢谢
I am using CLLocationManager
on device and the delegate methods aren't getting called. Here's my code (I can see that the class isn't getting deallocated either):
#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
In log, I see:
2011-10-02 19:49:04.095 DixieMatTracker[1404:707] Initialized
2011-10-02 19:49:04.109 DixieMatTracker[1404:707] Enabled
What am I doing wrong?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您使用的是
locationManager
ivar,而不是属性;这意味着CLLocationManager
不会被保留,并且会在您自动释放它时被释放。使用 self.locationManager :You're using the
locationManager
ivar, not the property; this means that theCLLocationManager
isn't being retained and will be deallocated as you're autoreleasing it. Useself.locationManager
:我建议您检查 CLLocationManager 实例的内存管理。
这样写:
委托不会被调用,因为 CLLocationManager 对象在提供对位置请求的响应之前已被释放。
这是由代码中的自动释放引起的。
除此之外 - 如果使用了 autorelease,那么在 dealloc 中手动释放它是错误的。这可能会导致意外崩溃。
上面的解决方案“手动”在 init 中分配 CLLocationManager 并在 dealloc 中释放它。
I would suggest that you check your memory management on your CLLocationManager instance.
Write it so:
The delegate does not get called because the CLLocationManager object is deallocated before it can provide a response to the location request.
This is caused by the autorelease in your code.
Besides that - iff autorelease is used it would be a mistake to manually release it in dealloc. This could lead to unexpected crashes.
The solution above does it 'by-hand' allocating a CLLocationManager in init and releasing it in dealloc.
就我而言,这是因为我的设备无法确定位置 - 当我待在室内时,GPS 信号不可用,Wi-Fi 也断开,因此位置管理器无法接收任何位置数据,并且从未呼叫代表。
一旦我连接了无线网络,它就可以工作了(我想在这种情况下移动到户外也应该可以解决问题,但有时这不是很方便的方法:))
In my case it was because of my device was unable to determine location - while staying indoors GPS signal was unavailable and wi-fi was also disconnected so location manager just wasn't able to receive any location data and delegate was never called.
Once I've connected wi-fi it worked (I guess moving outdoors should also do the trick in that case, but sometimes it is not very convenient way :) )