我在 iOS 5 中的 CoreLocation 区域委托(delegate)方法方面遇到了问题,无论是在模拟器中还是在设备上。我正在尝试添加一个监视区域,然后等待 didStartMonitoring
委托(delegate)回调。很少有,它工作得很好。但是,通常 didStartMonitoring
和 monitoringDidFail
都不会被调用。该区域确实被添加到 monitoredRegions
中。委托(delegate)对象设置正确,通常会调用 didEnterRegion
和 didExitRegion
。位置管理器永远不会被释放。这是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) locationManager:(CLLocationManager*)manager
didStartMonitoringForRegion:(CLRegion*)region
{
NSLog( @"region monitoring started" );
}
- (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" );
}
有人有什么想法吗?我可以处理这个问题,但是 didEnterRegion
和 didExitRegion
委托(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)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSLog( @"initializing location manager" );
self.locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
[locationManager startUpdatingLocation];
[self startMonitoringRegion];
[self performSelector:@selector(printMonitoredRegions) withObject:nil afterDelay:2.];
return YES;
}
- (void)locationManager:(CLLocationManager *)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 库吗?
最佳答案
看看苹果怎么说:
测试您的应用的区域监控支持
在 iOS 模拟器或设备上测试区域监控代码时,请意识到区域事件可能不会在跨越区域边界后立即发生。为了防止虚假通知,iOS 在满足某些阈值条件之前不会发送区域通知。具体来说,用户的位置必须跨越区域边界并远离该边界最小距离,并在报告通知之前保持在该最小距离至少 20 秒。
具体阈值距离由当前可用的硬件和定位技术确定。例如,如果 Wi-Fi 被禁用,区域监控的准确度就会显着降低。但是,出于测试目的,您可以假设最小距离约为 200 米。
关于ios - CoreLocation 区域委托(delegate)未调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9320185/