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

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

最佳答案

这里有一些要检查的东西:

  • 在开始监视代码中的区域之前,请调用[CLLocationManager regionMonitoringAvailable][CLLocationManager regionMonitoringEnabled]以确保该服务在用户电话上可用并已启用。
  • 确保已将位置管理器的delegate属性设置为已实现locationManager:didEnterRegion:和/或locationManager:didExitRegion:的对象。
  • 确保这些方法签名中没有错别字。小写大写错误将导致这些消息的传递失败。将它们复制/粘贴到您的代码中,并确保它们匹配:
    - (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
    { /* Handle enter */ }
    
    - (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
    { /* Handle exit */ }
    
  • 确保您的委托(delegate)还实现了locationManager:monitoringDidFailForRegion:withError:,因为它可能会告诉您服务失败的原因。
    - (void)locationManager:(CLLocationManager *)manager monitoringDidFailForRegion:(CLRegion *)region withError:(NSError *)error
    {
        NSLog(@"Region monitoring failed with error: %@", [error localizedDescription]);
    }
    
  • 可能发生此类监视失败的原因之一是,“核心位置”对允许应用监视的区域数量施加了限制。实际上,这个限制似乎每个应用程序大约有十个区域。因此,请确保使用stopMonitoringForRegion:删除不需要的区域,并按照Apple的Location Awareness Programming Guide的建议仅监视距离用户最近的那些区域:

  • 希望很明显,但请确保在设置委托(delegate)后调用startMonitoringForRegion:desiredAccuracy:
  • 使用CLRegion初始化要监视的initCircularRegionWithCenter:radius:identifier:对象时,请确保对每个区域使用唯一的标识符。
  • 如果在应用程序处于事件状态时正确调用了locationManager:didEnterRegion:locationManager:didExitRegion:方法,但在被杀死后操作系统在后台重新启动应用程序时却未正确调用,则在这种情况下,您可能无法正确初始化位置管理器并设置其委托(delegate)人。如果您在应用程序未运行时越过注册的区域边界,则操作系统将在后台启动您的应用程序,您可以使用应用程序委托(delegate)的if ([launchOptions objectForKey:@"UIApplicationLaunchOptionsLocationKey"]]) {}方法中的application:didFinishLaunchingWithOptions:进行检测。这样在后台启动应用程序时,您的应用程序可能不会加载任何 View ,因此您需要确保application:didFinishLaunchingWithOptions:调用一些实例化位置管理器对象的代码路径,并在这种情况下设置其委托(delegate)。设置位置经理的代表属性后,将立即发送任何待处理的区域监视事件。
  • 关于iphone - startMonitoringForRegion永远不会调用didEnterRegion/didExitRegion,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4152361/

    10-10 16:46