ios - CoreLocation 区域委托(delegate)未调用

标签 ios delegates core-location

我在 iOS 5 中的 CoreLocation 区域委托(delegate)方法方面遇到了问题,无论是在模拟器中还是在设备上。我正在尝试添加一个监视区域,然后等待 didStartMonitoring 委托(delegate)回调。很少有,它工作得很好。但是,通常 didStartMonitoringmonitoringDidFail 都不会被调用。该区域确实被添加到 monitoredRegions 中。委托(delegate)对象设置正确,通常会调用 didEnterRegiondidExitRegion。位置管理器永远不会被释放。这是main thread 。我已经检查了所有我能想到的条件。

-(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" );
}

有人有什么想法吗?我可以处理这个问题,但是 didEnterRegiondidExitRegion 委托(delegate)方法有时也不一致,这对我来说是一个大问题。

编辑:我可以在单个应用程序委托(delegate)中复制此功能——无需自定义对象或任何东西。请参阅下面的实现。区域被添加(打印时可见),但 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:我刚刚注意到 iOS implementation CLLocationManagerDelegate 协议(protocol)与 Mac implementation 略有不同-- 值得注意的是,Mac 没有 didStartMonitoringRegion。我可能会不小心使用 Mac 库而不是 iOS 库吗?

最佳答案

看看苹果怎么说:

http://developer.apple.com/library/ios/#documentation/userexperience/conceptual/LocationAwarenessPG/CoreLocation/CoreLocation.html

测试您的应用的区域监控支持

在 iOS 模拟器或设备上测试区域监控代码时,请意识到区域事件可能不会在跨越区域边界后立即发生。为了防止虚假通知,iOS 在满足某些阈值条件之前不会发送区域通知。具体来说,用户的位置必须跨越区域边界并远离该边界最小距离,并在报告通知之前保持在该最小距离至少 20 秒。

具体阈值距离由当前可用的硬件和定位技术确定。例如,如果 Wi-Fi 被禁用,区域监控的准确度就会显着降低。但是,出于测试目的,您可以假设最小距离约为 200 米。

关于ios - CoreLocation 区域委托(delegate)未调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9320185/

相关文章:

ios - RxSwift - 递归 Observables?

iphone - 无法正确设置 NSURLIsExcludedFromBackupKey

ios - Protocol Delegate 未按预期工作返回 nil swift

ios - 我可以用 mapkit 获取商店名称/餐厅名称吗?(swift)

objective-c - CoreLocation , MapKit 错误

javascript - 在 Javascript 中检测 Ipad Iphone 上的隐藏键盘按钮

objective-c - 比较两个日期

c# - 从使用委托(delegate)搜索不同属性的对象中返回属性?

ios - 什么时候应该使用数据源(协议(protocol))与在属性或初始化上设置类数据?

c# - CLGeocoder.ReverseGecode 在 50 个请求后挂起