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) locationManagerCLLocationManager*)manager
didStartMonitoringForRegionCLRegion*)region
{
NSLog( @"region monitoring started" );
}
- (void) locationManagerCLLocationManager *)manager
monitoringDidFailForRegionCLRegion *)region
withErrorNSError *)error
{
NSLog( @"region monitoring failed" );
}
- (void)locationManagerCLLocationManager *)manager didFailWithErrorNSError *)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)applicationUIApplication *)application didFinishLaunchingWithOptionsNSDictionary *)launchOptions
{
NSLog( @"initializing location manager" );
self.locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
[locationManager startUpdatingLocation];
[self startMonitoringRegion];
[self performSelectorselector(printMonitoredRegions) withObject:nil afterDelay:2.];
return YES;
}
- (void)locationManagerCLLocationManager *)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/
欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://jike.in/) | Powered by Discuz! X3.4 |