为什么didEnterRegion叫了两次?

pra*_*bhu 6 iphone cllocationmanager ios clregion

我在我的iOS应用程序中使用位置服务即区域监控,这是我的代码

//this for creating region
-(void)createRegion
{
    [dictionary setValue:@"23 St, New York" forKey:@"title"];
    [dictionary setValue:[NSNumber numberWithDouble:40.742878] forKey:@"latitude"];
    [dictionary setValue:[NSNumber numberWithDouble:-73.992821] forKey:@"longitude"];
    [dictionary setValue:[NSNumber numberWithDouble:(300.0)] forKey:@"radious"];

    [regionArray addObject:[self mapDictionaryToRegion:dictionary]];
    [self initializeRegionMonitoring:regionArray];
}
- (CLRegion*)mapDictionaryToRegion:(NSDictionary*)dictionary {
    NSString *title = [dictionary valueForKey:@"title"];

    CLLocationDegrees latitude = [[dictionary valueForKey:@"latitude"] doubleValue];
    CLLocationDegrees longitude =[[dictionary valueForKey:@"longitude"] doubleValue];
    CLLocationCoordinate2D centerCoordinate = CLLocationCoordinate2DMake(latitude, longitude);

    CLLocationDistance regionRadius = [[dictionary valueForKey:@"radius"] doubleValue];

    return [[CLRegion alloc] initCircularRegionWithCenter:centerCoordinate
                                                   radius:regionRadius
                                               identifier:title];
}

- (void) initializeRegionMonitoring:(NSArray*)geofences {
    if(![CLLocationManager regionMonitoringAvailable]) {
       // [self showAlertWithMessage:@"This app requires region monitoring features which are unavailable on this device."];
        return;
    }

    for(CLRegion *geofence in geofences) {
        [_locationManager startMonitoringForRegion:geofence];
    }
}

- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region {
NSLog(@"entered region %@",region.identifier);
}
- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region {
    NSLog(@"exited region %@",region.identifier);
}
Run Code Online (Sandbox Code Playgroud)

当app在前台时,它工作正常.它向我显示了日志:"输入区域.."和"退出区域..",但是当应用程序转到后台时,相同的日志会在几分之一秒内打印两次,即委托方法调用两次,我不需要,有什么办法可以避免两次打电话吗? 或者在创建或监控区域时是否有任何错误?请帮帮我..先谢谢..

Bil*_*ess 9

我相信这个问题与我自己的问题相同.我认为Apple的区域监控API存在一个错误.我已经向Apple提出了这个问题的雷达,但到目前为止还没有收到任何回复.

解决这个问题的一种方法是在didEnter和didExit方法中保存时间戳,如果方法在保存的时间戳的10秒内触发,则只需跳过该方法作为欺骗.

如果有人有兴趣,我在github上有一个项目显示这个问题.

https://github.com/billburgess/GeofenceBug

随意提交另一个雷达,因为这是Apple注意到问题并采取行动的唯一方式.雷达编号为12452255 - 重复代表呼叫进行区域监控.

如果您想要使用这种雷达,这里是开放式雷达链接信息. http://openradar.appspot.com/radar?id=2484401