gpt4 book ai didi

ios - 未调用 CoreLocation 区域委托(delegate)

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:17:06 25 4
gpt4 key购买 nike

我在使用 iOS 5 中的 CoreLocation 区域委托(delegate)方法时遇到问题,无论是在模拟器中还是在设备上。我正在尝试添加一个监控区域,然后等待 didStartMonitoring 委托(delegate)回调。很少,它工作正常。但是,通常 didStartMonitoringmonitoringDidFail 都不会被调用。该区域会添加到 monitoredRegions。委托(delegate)对象设置正确,通常会调用 didEnterRegiondidExitRegion。位置管理器永远不会被释放。这是在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" );
}

有人有什么想法吗?我可以处理这个问题,但似乎 didEnterRegiondidExitRegion 委托(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 库?

最佳答案

看看 Apple 是怎么说的:

http://developer.apple.com/library/ios/#documentation/userexperience/conceptual/LocationAwarenessPG/CoreLocation/CoreLocation.html

测试您的应用的区域监控支持

在 iOS 模拟器或设备上测试区域监控代码时,要意识到区域事件可能不会在区域边界被跨越后立即发生。为防止虚假通知,iOS 在满足特定阈值条件之前不会发送区域通知。具体而言,用户的位置必须跨越区域边界并离开该边界最小距离,并在报告通知之前保持该最小距离至少 20 秒。

具体的阈值距离由硬件和当前可用的定位技术决定。例如,如果 Wi-Fi 被禁用,区域监控的准确性就会大大降低。但是,出于测试目的,您可以假设最小距离约为 200 米。

关于ios - 未调用 CoreLocation 区域委托(delegate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9320185/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com