• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

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

[复制链接]
菜鸟教程小白 发表于 2022-12-13 11:42:38 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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)     locationManagerCLLocationManager*)manager 
didStartMonitoringForRegionCLRegion*)region
{
   NSLog( @"region monitoring started" );
}

- (void)   locationManagerCLLocationManager *)manager
monitoringDidFailForRegionCLRegion *)region 
                 withErrorNSError *)error
{
   NSLog( @"region monitoring failed" );
}

- (void)locationManagerCLLocationManager *)manager didFailWithErrorNSError *)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)applicationUIApplication *)application didFinishLaunchingWithOptionsNSDictionary *)launchOptions
{
   NSLog( @"initializing location manager" );
   self.locationManager = [[CLLocationManager alloc] init];
   locationManager.delegate = self;
   [locationManager startUpdatingLocation];

   [self startMonitoringRegion];
   [self performSelectorselector(printMonitoredRegions) withObject:nil afterDelay:2.];

   return YES;
}


- (void)locationManagerCLLocationManager *)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 库?



Best Answer-推荐答案


看看苹果怎么说:

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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap