CLLocationManager 不调用任何委托方法

发布于 2024-12-05 09:12:23 字数 2320 浏览 1 评论 0原文

首先,免责声明是我的目标是 iOS 5,所以这很可能是我的问题的根源,但如果不是……

我正在尝试编写一个简单的类,通过 CoreLocation 管理位置更新。然而,我看到一些非常奇怪的行为。我有一个自定义类,它基本上包装了 CLLocationManager 和委托方法。这是接口:

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

@protocol CLZipCodeFetcherDelegate
@optional - (void)didReceiveZipCode:(NSString *)zipCode;
@end

@interface CLZipCodeFetcher : NSObject <CLLocationManagerDelegate> {
    id <CLZipCodeFetcherDelegate> delegate;
    CLLocationManager *locationManager;
}

@property (strong, readwrite) id <CLZipCodeFetcherDelegate> delegate;
@property (strong, read write) CLLocationManager *locationManager;

- (void)getZipCode;

@end

和实现(忽略邮政编码相关的东西 - 这就是该类最终要做的事情,但现在我只是想从 CoreLocation 获取一些坐标):

#import "CLZipCodeFetcher.h"

@implementation CLZipCodeFetcher

@synthesize delegate, locationManager;

- (id) init {
    self = [super init];
    if (self != nil) {
        self.locationManager = [[CLLocationManager alloc] init];
        self.locationManager.delegate = self;
        self.locationManager.distanceFilter = 10.0f; // we don't need to be any more accurate than 10m
        self.locationManager.desiredAccuracy = kCLLocationAccuracyThreeKilometers;
        self.locationManager.purpose = @"Retrieve zip code";
    }
    return self;
}



- (void)getZipCode {
    [self.locationManager startUpdatingLocation];
}



- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    NSLog(@"Received location");
}



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

@end

然后我初始化一个实例我的视图控制器中的类并调用该方法:

CLZipCodeFetcher *zipCodeFetcher = [[CLZipCodeFetcher alloc] init];
[zipCodeFetcher getZipCode];

现在,当我运行它时,应用程序需要一些时间来加载,然后弹出警报对话框,请求允许应用程序使用位置服务。然而,它一出现,就立即消失(速度太快,让我无法点击允许或拒绝按钮),并且什么也没有发生(没有调用委托方法)。如果我随后进入设备的位置设置,它确实会为我的应用程序显示“关闭”。当我从该屏幕手动打开它,然后再次尝试该应用程序时,没有出现警报对话框,但仍然没有任何反应。我的调试消息都没有被记录。模拟器和我的 iPhone 4 上的行为是相同的。

所以基本上,有人知道为什么吗?我已经查看了所有类似的线程,许多问题都与内存管理有关,但我认为这不是问题,因为 ARC 应该管理我的 CLLocationManager 对象,对吗?

PS 另外,我确实将 CoreLocation 框架添加到了我的项目中,并且没有收到任何代码错误或警告,但是在我的应用程序中使用 CoreLocation 之前我还需要做些什么吗?

First off, the disclaimer is I'm targeting iOS 5, so that very well may be the source of my issues, but if not...

I'm trying to write a simple class that manages location updates through CoreLocation. However, I see some pretty strange behavior. I have a custom class which basically wraps CLLocationManager and the delegate methods. This is the interface:

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

@protocol CLZipCodeFetcherDelegate
@optional - (void)didReceiveZipCode:(NSString *)zipCode;
@end

@interface CLZipCodeFetcher : NSObject <CLLocationManagerDelegate> {
    id <CLZipCodeFetcherDelegate> delegate;
    CLLocationManager *locationManager;
}

@property (strong, readwrite) id <CLZipCodeFetcherDelegate> delegate;
@property (strong, read write) CLLocationManager *locationManager;

- (void)getZipCode;

@end

and the implementation (ignore the zip code related stuff- that's what the class is eventually meant to do, but right now I'm just trying to get some coordinates back from CoreLocation):

#import "CLZipCodeFetcher.h"

@implementation CLZipCodeFetcher

@synthesize delegate, locationManager;

- (id) init {
    self = [super init];
    if (self != nil) {
        self.locationManager = [[CLLocationManager alloc] init];
        self.locationManager.delegate = self;
        self.locationManager.distanceFilter = 10.0f; // we don't need to be any more accurate than 10m
        self.locationManager.desiredAccuracy = kCLLocationAccuracyThreeKilometers;
        self.locationManager.purpose = @"Retrieve zip code";
    }
    return self;
}



- (void)getZipCode {
    [self.locationManager startUpdatingLocation];
}



- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    NSLog(@"Received location");
}



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

@end

And then I initialize an instance of the class in my view controller and call the method:

CLZipCodeFetcher *zipCodeFetcher = [[CLZipCodeFetcher alloc] init];
[zipCodeFetcher getZipCode];

Now, when I run this, the app takes a moment to load, and then pops up the alert dialog asking for permission to let the app use location services. However, as soon as it appears, it immediately disappears (too fast to let me hit the allow or deny button), and nothing happens (no delegate methods are called). If I then go into the Location Settings of the device, it indeed shows 'Off' for my app. When I turn it on manually from that screen and then go to try the app again, no alert dialog appears, but still nothing happens. Neither of my debug messages are logged. The behavior is the same on both the simulator and my iPhone 4.

So basically, anyone have any idea why? I've looked through all the similar threads, and many issues were related to memory management, but I don't think that'd be a problem here since ARC should be managing my CLLocationManager object, right?

P.S. Also, I did add the CoreLocation framework to my project, and I get no code errors or warnings, but is there anything else I need to do before using CoreLocation in my app?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

回忆凄美了谁 2024-12-12 09:12:23

ARC 在 getZipCode 方法调用后释放新创建的对象。

ARC is deallocating your newly created object after the getZipCode method call.

烟沫凡尘 2024-12-12 09:12:23

我还没有使用过 ARC,所以我不确定标头中的语法(读写与读写)。

但我会放一个 NSLog(@" my loc manager is %@", self.locationManager);在 getZipCode 内部只是为了确保您拥有一个并且在它有机会执行任何操作之前不会被释放。

I have not used ARC yet so I'm not sure of your syntax in the header (readwrite vs. read write).

But I would put an NSLog(@" my loc manager is %@", self.locationManager); inside of getZipCode just to make sure you've got one and it's not being released before it has a chance to do anything.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文