我在模擬器和設備上都遇到iOS 5中CoreLocation區域委託方法的問題。我正在嘗試添加一個用於監視的區域,然後等待didStartMonitoring
委託回調。很少,它工作正常。但是,通常不會調用didStartMonitoring
和monitoringDidFail
。該地區確實被添加到monitoredRegions
。委託對象設置正確,通常會撥打didEnterRegion
和didExitRegion
。位置管理員從未被釋放。這是在main thread。我檢查了我能想到的所有條件。CoreLocation區域代理不叫
-(id) init
{
self = [super init];
if(self) {
NSLog(@"initializing location manager");
self.locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
[locationManager startUpdatingLocation];
}
return self;
}
-(void) startMonitoringRegion
{
BOOL monitoring = NO;
if ([CLLocationManager regionMonitoringAvailable]) {
if ([CLLocationManager regionMonitoringEnabled]) {
if([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorized) {
monitoring = YES;
} else {
NSLog(@"app is not authorized for location monitoring");
}
} else {
NSLog(@"region monitoring is not enabled");
}
} else {
NSLog(@"region monitoring is not available");
}
if(!monitoring) return;
CLRegion *region = [[CLRegion alloc] initCircularRegionWithCenter:locationManager.location.coordinate
radius:50
identifier:@"majorRegion"];
NSLog(@"trying to start monitoring for region %@", region);
[locationManager startMonitoringForRegion:region desiredAccuracy:kCLLocationAccuracyBest];
}
-(void) locationManager:(CLLocationManager*)manager
didStartMonitoringForRegion:(CLRegion*)region
{
NSLog(@"region monitoring started");
}
- (void) locationManager:(CLLocationManager *)manager
monitoringDidFailForRegion:(CLRegion *)region
withError:(NSError *)error
{
NSLog(@"region monitoring failed");
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"location manager failed");
}
任何人有什麼想法?我可以處理這個問題,但看起來didEnterRegion
和didExitRegion
委託方法有時也不一致,這對我來說是個大問題。
編輯:我可以在一個應用程序委託中複製這個功能 - 沒有自定義對象或任何東西。見下面的實現。區域被添加(打印時看到),但didStartMonitoringRegion
永遠不會被調用。
@implementation AppDelegate
@synthesize window = _window;
@synthesize locationManager;
-(void) startMonitoringRegion
{
BOOL monitoring = NO;
if ([CLLocationManager regionMonitoringAvailable]) {
if ([CLLocationManager regionMonitoringEnabled]) {
if([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorized) {
monitoring = YES;
} else {
NSLog(@"app is not authorized for location monitoring");
}
} else {
NSLog(@"region monitoring is not enabled");
}
} else {
NSLog(@"region monitoring is not available");
}
if(!monitoring) return;
CLRegion *region = [[CLRegion alloc] initCircularRegionWithCenter:locationManager.location.coordinate
radius:50.
identifier:@"majorRegion"];
NSLog(@"trying to start monitoring for region %@", region);
[locationManager startMonitoringForRegion:region desiredAccuracy:kCLLocationAccuracyBest];
}
-(void) printMonitoredRegions
{
NSLog(@"printing regions:");
for(CLRegion* region in locationManager.monitoredRegions)
NSLog(@"%@", region);
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSLog(@"initializing location manager");
self.locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
[locationManager startUpdatingLocation];
[self startMonitoringRegion];
[self performSelector:@selector(printMonitoredRegions) withObject:nil afterDelay:2.];
return YES;
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
//NSLog(@"location updated");
}
-(void) locationManager:(CLLocationManager*)manager
didStartMonitoringForRegion:(CLRegion*)region
{
NSLog(@"region monitoring started");
}
-(void) locationManager:(CLLocationManager*)manager didEnterRegion:(CLRegion*)region
{
NSLog(@"did enter region");
}
-(void) locationManager:(CLLocationManager*)manager didExitRegion:(CLRegion*)region
{
NSLog(@"did exit region");
}
- (void) locationManager:(CLLocationManager *)manager
monitoringDidFailForRegion:(CLRegion *)region
withError:(NSError *)error
{
NSLog(@"region monitoring failed");
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"location manager failed");
}
@end
登錄:
2012-02-21 10:53:50.397 locationtest[64440:f803] initializing location manager
2012-02-21 10:53:50.412 locationtest[64440:f803] trying to start monitoring for region (identifier majorRegion) <LAT,LONG> radius 50.00m
2012-02-21 10:53:52.414 locationtest[64440:f803] printing regions:
2012-02-21 10:53:52.416 locationtest[64440:f803] (identifier majorRegion <LAT,LONG> radius 50.00m
編輯2:我剛剛注意到CLLocationManagerDelegate
協議iOS implementation從Mac implementation略有不同 - 值得注意的是,蘋果並沒有didStartMonitoringRegion
。有沒有什麼方法我不小心使用Mac庫而不是iOS庫?
對不起,我意識到,固定它,我張貼後。它沒有幫助。 – jab 2012-02-20 22:06:40
無論如何,我問是不是檢測區域進入/退出的問題,這就是爲什麼'didStartMonitoringForRegion'委託方法不會被調用(從來沒有了,但我發誓我看到了它的工作一次或兩次)。 – jab 2012-02-20 22:10:10
重新您的更新:我已經做了好辦法,但在此實現,'startMonitoringRegion'被稱爲對象初始化後立即(在'init'方法完成後右)。在那裏添加延遲沒有幫助。 – jab 2012-02-21 18:45:35