gpt4 book ai didi

iOS 7 核心位置 : region monitoring fails on the first time after location services are authorised

转载 作者:可可西里 更新时间:2023-11-01 03:30:02 34 4
gpt4 key购买 nike

我使用 CoreLocation 在我的应用程序上发现了一个奇怪的行为。我正在使用区域监控功能,但在授权位置服务(通过弹出窗口或设置 -> 位置服务)后,区域监控失败(操作无法完成。kCLErrorDomain 错误 5.)。如果我关闭应用程序并重新启动(因此已经获得授权),一切都会按预期进行。我的代码如下所示:

-(void)initializeLocationServices
{
NSLog(@"Started location services");

locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
locationManager.pausesLocationUpdatesAutomatically = NO;

[locationManager startUpdatingLocation]; // to show authorisation popup
}

-(CLCircularRegion*)createRegion
{
// Test coordinates
CLLocationDegrees latitude = 50;
CLLocationDegrees longitude = -1;
CLLocationDistance radius = 50; // meters;

// If radius is too large, registration fails automatically, so limit the radius to the maximum value
if (radius > locationManager.maximumRegionMonitoringDistance) {
radius = locationManager.maximumRegionMonitoringDistance;
}

CLCircularRegion* region = [[CLCircularRegion alloc] initWithCenter:CLLocationCoordinate2DMake(latitude, longitude) radius:radius identifier:@"TEST"];

region.notifyOnEntry = YES;
region.notifyOnExit = YES;

NSLog(@"Created region");

return region;
}

-(void)monitorProximity
{
CLRegion *region = [self createRegion];

// Check if support is unavailable
if ( ![CLLocationManager isMonitoringAvailableForClass:[CLRegion class]]) {
NSLog( @"Failed to initialise region monitoring: support unavailable");
return;
}

// Check if authorised
if ([CLLocationManager authorizationStatus] != kCLAuthorizationStatusAuthorized) {
NSLog( @"Failed to initialise region monitoring: app not authorized to use location services");
return;
} else {
NSLog(@"Started monitoring proximity");
}


// Clear out any old regions to prevent buildup.
if ([locationManager.monitoredRegions count] > 0) {
for (id obj in locationManager.monitoredRegions)
[locationManager stopMonitoringForRegion:obj];
}

[locationManager startMonitoringForRegion:region];
}

-(void)locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region
{
NSLog(@"Started monitoring for region: %@", [region description]);
[locationManager requestStateForRegion:region]; // check if already inside region
}

-(void)locationManager:(CLLocationManager *)manager monitoringDidFailForRegion:(CLRegion *)region withError:(NSError *)error
{
NSLog(@"Failed to start monitoring for region: %@", [error localizedDescription]);
}


-(void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region
{
NSLog(@"didDetermineState");

if (state == CLRegionStateInside) {

NSLog(@"inside");
return;


} else if (state == CLRegionStateOutside) {
NSLog(@"outside");

} else {
NSLog(@"unknown");
}

}

-(void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
{
NSLog(@"didEnterRegion");
}

-(void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
{
NSLog(@"didExitRegion");
}

-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
NSLog(@"Monitoring authorisation status is now: %@", status == kCLAuthorizationStatusAuthorized ? @"authorized" : @"not authorized");

if (status == kCLAuthorizationStatusAuthorized) {
[self monitorProximity];
}
}

我是不是做错了什么?调用 didChangeAuthorizationStatus 后流程是否有问题?

最佳答案

来自 other user reports ,似乎 kCLErrorDomain 5 是区域监控失败的“包罗万象”;它没有提供太多有用的信息。我相信您的问题是由线路引起的

[locationManager requestStateForRegion:region]; // check if already inside region

您从委托(delegate)方法 didStartMonitoringForRegion: 中调用它

我在自己的项目中看到了一些非常相似的东西,把这条线去掉(或延迟执行一段时间)解决了这个问题。我最好的猜测是,当此委托(delegate)方法触发时,iOS 仍在运行一些内部区域监视代码,因此现在不是调用 requestStateForRegion:

的合适时间

试着把这个拿出来,看看是不是答案。

关于iOS 7 核心位置 : region monitoring fails on the first time after location services are authorised,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22404620/

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