iphone - startMonitoringForRegion 从不调用 didEnterRegion/didExitRegion

标签 iphone monitoring core-location region

我尝试让iPhone4监视区域并通过调用didEnterRegion或didExitRegion通知我。我无法让它工作。我在这里阅读了可能所有相关的条目,再加上网上的几篇文章....iOS 只是不调用我的 CLLocationManagerDelegate 方法。 我做了什么:

我有一个简单的 AppDelegate,它还实现 didEnterRegion 和 didExitRegion 的 CLLocationManagerDelegate 方法。在这些方法中,我只需使用 UILocalNotification 来报告事件。我从 ViewController 创建一个半径为 1000 米的区域(当前位置)。

最佳答案

以下是一些需要检查的事项:

  1. 在开始监视代码中的区域之前,请调用 [CLLocationManager regionMonitoringAvailable][CLLocationManager regionMonitoringEnabled]确保该服务在用户的手机上可用并已启用。

  2. 确保您拥有位置管理员的 delegate属性设置为您已实现的对象 locationManager:didEnterRegion:和/或 locationManager:didExitRegion: .

  3. 确保这些方法签名中没有任何拼写错误。一个小的大小写错误就会导致这些消息的传递失败。将它们复制/粘贴到您的代码中并确保它们匹配:

    - (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
    { /* Handle enter */ }
    
    - (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
    { /* Handle exit */ }
    
  4. 确保您的委托(delegate)也实现 locationManager:monitoringDidFailForRegion:withError: ,因为它可能会告诉您服务失败的原因。

    - (void)locationManager:(CLLocationManager *)manager monitoringDidFailForRegion:(CLRegion *)region withError:(NSError *)error
    {
        NSLog(@"Region monitoring failed with error: %@", [error localizedDescription]);
    }
    
  5. 可能发生此类监控失败的原因之一是 Core Location 对允许应用监控的区域数量施加了限制。实际上,这个限制似乎是每个应用程序大约十个区域。因此,请确保使用 stopMonitoringForRegion: 删除不需要的区域。 ,并按照 Apple Location Awareness Programming Guide 的建议仅监控距离用户最近的区域。 :

    You should always be judicious when specifying the set of regions to monitor. Regions are a shared system resource and the total number of regions available systemwide is limited. For this reason, Core Location limits the number of regions that may be simultaneously monitored by a single application. To work around these limits, you should consider registering only those regions in the user’s immediate vicinity. As the user’s location changes, you can remove regions that are now farther way and add regions coming up on the user’s path. If you attempt to register a region and space is unavailable, the location manager calls the locationManager:monitoringDidFailForRegion:withError: method of its delegate with the kCLErrorRegionMonitoringFailure error code.

  6. 希望很明显,但请确保您调用 startMonitoringForRegion:desiredAccuracy:设置您的委托(delegate)后。

  7. 当您初始化CLRegion时您正在使用 initCircularRegionWithCenter:radius:identifier: 监视的对象,请确保为每个区域使用唯一的标识符。

  8. 如果您的locationManager:didEnterRegion:locationManager:didExitRegion:当应用程序处于事件状态时,方法会被正确调用,但当操作系统在应用程序被终止后在后台重新启动应用程序时,方法会被正确调用,那么在这种情况下,您可能无法正确初始化位置管理器并设置其委托(delegate)。如果您在应用程序未运行时跨越注册的区域边界,操作系统将在后台启动您的应用程序,您可以使用 if ([launchOptions objectForKey:@"UIApplicationLaunchOptionsLocationKey"]]) {} 进行检测。在 application:didFinishLaunchingWithOptions:您的应用程序委托(delegate)的方法。当您的应用程序像这样在后台启动时,可能不会加载任何 View ,因此您需要确保 application:didFinishLaunchingWithOptions:在这种情况下,调用一些代码路径来实例化您的位置管理器对象并设置其委托(delegate)。一旦设置了位置管理器的委托(delegate)属性,任何待处理的区域监控事件都会被传递。

关于iphone - startMonitoringForRegion 从不调用 didEnterRegion/didExitRegion,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4152361/

相关文章:

bash - 无法使用 bash 作为 Sensu 插件获取 jstat 指标

iphone - iPhone-惯性拖动

iOS:不同 Storyboard中的 presentViewController 不显示导航栏

powershell - 将警报规则添加到 Azure 云服务时出错

sql - 如何监控SQL Server 2005上执行的sql语句

ios - 查找 CLLocationCoordinate2D 点之间的距离

iphone - 是否可以使用 AVAudioPlayer 播放 mp3 文件的一部分?如果是,如何?

iphone - 查看我的免费 iPhone 应用程序被下载了多少次

ios - CLLocation 管理器 startUpdatingLocation 未启动

ios - 即使在暂停时也能获取 iOS 应用程序的位置更新