我尝试让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 */ }
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的建议仅监视距离用户最近的那些区域: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/