菜鸟教程小白 发表于 2022-12-13 11:42:38

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


                                            <p><p>iOS 5 中的 CoreLocation 区域委托(delegate)方法在模拟器和设备上都有问题。我正在尝试添加一个区域进行监控,然后等待 <code>didStartMonitoring</code> 委托(delegate)回调。很少,它工作正常。但是,通常不会调用 <code>didStartMonitoring</code> 和 <code>monitoringDidFail</code>。该区域确实被添加到 <code>monitoredRegions</code>。委托(delegate)对象设置正确,通常会调用 <code>didEnterRegion</code> 和 <code>didExitRegion</code>。位置管理器永远不会被释放。这是在 <a href="https://stackoverflow.com/questions/3731881/why-is-my-cllocationmanager-delegate-not-getting-called" rel="noreferrer noopener nofollow">main thread</a> .我已经检查了我能想到的所有条件。</p>

<pre><code>-(id) init
{
   self = ;
   if( self ) {
      NSLog( @&#34;initializing location manager&#34; );
      self.locationManager = [ init];
      locationManager.delegate = self;
      ;
   }
   return self;
}

-(void) startMonitoringRegion
{
   BOOL monitoring = NO;   
   if ( ) {
      if ( ) {
         if( == kCLAuthorizationStatusAuthorized ) {
            monitoring = YES;
         } else {
            NSLog( @&#34;app is not authorized for location monitoring&#34; );
         }
      } else {
         NSLog( @&#34;region monitoring is not enabled&#34; );
      }
   } else {
      NSLog( @&#34;region monitoring is not available&#34; );
   }
   if( !monitoring ) return;

   CLRegion *region = [ initCircularRegionWithCenter:locationManager.location.coordinate
                                                          radius:50
                                                      identifier:@&#34;majorRegion&#34;];
   NSLog( @&#34;trying to start monitoring for region %@&#34;, region );
   ;
}

-(void)   locationManager:(CLLocationManager*)manager
didStartMonitoringForRegion:(CLRegion*)region
{
   NSLog( @&#34;region monitoring started&#34; );
}

- (void)   locationManager:(CLLocationManager *)manager
monitoringDidFailForRegion:(CLRegion *)region
               withError:(NSError *)error
{
   NSLog( @&#34;region monitoring failed&#34; );
}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
   NSLog( @&#34;location manager failed&#34; );
}
</code></pre>

<p>有人有什么想法吗?我可以处理这个问题,但似乎 <code>didEnterRegion</code> 和 <code>didExitRegion</code> 委托(delegate)方法有时也不一致,这对我来说是个大问题。</p>

<p>编辑:我可以在单个应用程序委托(delegate)中复制此功能——无需自定义对象或任何东西。请参阅下面的实现。区域被添加(打印时看到),但 <code>didStartMonitoringRegion</code> 永远不会被调用。</p>

<pre><code>@implementation AppDelegate

@synthesize window = _window;
@synthesize locationManager;

-(void) startMonitoringRegion
{
   BOOL monitoring = NO;   
   if ( ) {
      if ( ) {
         if( == kCLAuthorizationStatusAuthorized ){
            monitoring = YES;
         } else {
            NSLog( @&#34;app is not authorized for location monitoring&#34; );
         }
      } else {
         NSLog( @&#34;region monitoring is not enabled&#34; );
      }
   } else {
      NSLog( @&#34;region monitoring is not available&#34; );
   }
   if( !monitoring ) return;

   CLRegion *region = [ initCircularRegionWithCenter:locationManager.location.coordinate
                                                            radius:50.
                                                          identifier:@&#34;majorRegion&#34;];
   NSLog( @&#34;trying to start monitoring for region %@&#34;, region );
   ;
}

-(void) printMonitoredRegions
{
   NSLog( @&#34;printing regions:&#34; );
   for( CLRegion* region in locationManager.monitoredRegions )
      NSLog( @&#34;%@&#34;, region );
}


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
   NSLog( @&#34;initializing location manager&#34; );
   self.locationManager = [ init];
   locationManager.delegate = self;
   ;

   ;
   ;

   return YES;
}


- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
         fromLocation:(CLLocation *)oldLocation
{
   //NSLog( @&#34;location updated&#34; );
}

-(void)   locationManager:(CLLocationManager*)manager
didStartMonitoringForRegion:(CLRegion*)region
{
   NSLog( @&#34;region monitoring started&#34; );
}

-(void) locationManager:(CLLocationManager*)manager didEnterRegion:(CLRegion*)region
{
   NSLog( @&#34;did enter region&#34; );
}

-(void) locationManager:(CLLocationManager*)manager didExitRegion:(CLRegion*)region
{
   NSLog( @&#34;did exit region&#34; );
}

- (void)   locationManager:(CLLocationManager *)manager
monitoringDidFailForRegion:(CLRegion *)region
               withError:(NSError *)error
{
   NSLog( @&#34;region monitoring failed&#34; );
}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
   NSLog( @&#34;location manager failed&#34; );
}

@end
</code></pre>

<p>日志:</p>

<pre><code>2012-02-21 10:53:50.397 locationtest initializing location manager
2012-02-21 10:53:50.412 locationtest trying to start monitoring for region (identifier majorRegion) &lt;LAT,LONG&gt; radius 50.00m
2012-02-21 10:53:52.414 locationtest printing regions:
2012-02-21 10:53:52.416 locationtest (identifier majorRegion &lt;LAT,LONG&gt; radius 50.00m
</code></pre>

<p>编辑 2:我刚刚注意到 <a href="https://developer.apple.com/library/ios/#documentation/CoreLocation/Reference/CLLocationManagerDelegate_Protocol/CLLocationManagerDelegate/CLLocationManagerDelegate.html#//apple_ref/doc/uid/TP40007124" rel="noreferrer noopener nofollow">iOS implementation</a> <code>CLLocationManagerDelegate</code> 协议(protocol)与 <a href="https://developer.apple.com/library/mac/#documentation/CoreLocation/Reference/CLLocationManagerDelegate_Protocol/CLLocationManagerDelegate/CLLocationManagerDelegate.html#//apple_ref/doc/uid/TP40007124" rel="noreferrer noopener nofollow">Mac implementation</a> 略有不同。 -- 值得注意的是,Mac 没有 <code>didStartMonitoringRegion</code>。有没有什么方法我不小心使用了 Mac 库而不是 iOS 库?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>看看苹果怎么说:</p>

<p> <a href="http://developer.apple.com/library/ios/#documentation/userexperience/conceptual/LocationAwarenessPG/CoreLocation/CoreLocation.html" rel="noreferrer noopener nofollow">http://developer.apple.com/library/ios/#documentation/userexperience/conceptual/LocationAwarenessPG/CoreLocation/CoreLocation.html</a> </p>

<p><strong>测试您应用的区域监控支持</strong></p>

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

<p><em>具体的阈值距离由硬件和当前可用的定位技术决定。例如,如果禁用 Wi-Fi,则区域监控的准确性会大大降低。但是,出于测试目的,您可以假设最短距离约为 200 米。</em></p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 未调用 CoreLocation 区域委托(delegate),我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/9320185/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/9320185/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 未调用 CoreLocation 区域委托(delegate)