didExitRegion не вызывается, циклы didEnterRegion

#ios #iphone #ios7 #region #clregion

#iOS #iPhone #ios7 #регион #clregion

Вопрос:

Я довольно новичок в разработке iOS и пытаюсь создать приложение, которое позволяет осуществлять мониторинг региона. Аналогично приложению напоминаний iOS.

У меня есть свой код до такой степени, что я начинаю мониторинг регионов, и он должен уведомлять меня, когда я вхожу в регион и покидаю его. Однако он сообщает мне только тогда, когда я нахожусь в регионе. Когда я ухожу, он мне не сообщает.

Просто чтобы вы знали, я тестирую это на своем iPhone и определенно вышел за пределы радиуса 100 м (я ехал на своей машине в течение 10 минут от моего дома). Он продолжал зацикливать сообщения о том, что я нахожусь в регионе, но никогда не менялся, когда я выходил из региона. Я действительно в тупике относительно того, где я ошибаюсь. Кто-нибудь может помочь? Кроме того, просто из любопытства, поскольку я новичок в разработке iOS, правильно ли я это делаю? Или есть более эффективный способ добиться этого? Любые советы, руководства, инструкции были бы очень признательны. Спасибо, ребята! Мой код для запуска мониторинга региона и методы приведены ниже. Дайте мне знать, если вам нужен какой-либо другой код.

 (IBAction)addRegion:(id)sender {
    if ([CLLocationManager isMonitoringAvailableForClass:[CLBeaconRegion class]]) {
        // Create a new region based on the center of the map view.
        CLLocationCoordinate2D coord = CLLocationCoordinate2DMake(_mapView.centerCoordinate.latitude, _mapView.centerCoordinate.longitude);
        region = [[CLCircularRegion alloc] initWithCenter:coord
                                           radius:Radius
                                       identifier:[NSString stringWithFormat:@"%f, %f", _mapView.centerCoordinate.latitude, _mapView.centerCoordinate.longitude]];


        // Start monitoring the newly created region.
        [_locationManager startMonitoringForRegion:(CLRegion *)region];
    }
    else {
     NSLog(@"Region monitoring is not available.");
    }
}

(IBAction)stopMonitoringRegion:(id)sender {
   [_locationManager stopMonitoringForRegion:(CLRegion *)region];
}

(void)locationManager:(CLLocationManager *)manager
     didDetermineState:(CLRegionState)state forRegion:(CLRegion *)reg {

   if (state == CLRegionStateInside) {
       //call didEnterRegion
       //[_locationManager stopMonitoringForRegion:region];
       [self locationManager:_locationManager didEnterRegion:region];
   }
   if (state == CLRegionStateOutside) {
       //call didExitRegion
       //[_locationManager stopMonitoringForRegion:region];
       [self locationManager:_locationManager didExitRegion:region];
   }
}

(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    NSLog(@"didFailWithError: %@", error);
}


(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {

//requestStateForRegion
if (region == nil) {
    //do nothing
    NSLog(@"didUpdateToLocation %@ from %@", newLocation, oldLocation);
}else{
[_locationManager requestStateForRegion:region];
    NSLog(@"didUpdateToLocation %@ from %@", newLocation, oldLocation);
}
}


(void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region  {
    NSLog(@"didEnterRegion %@ at %@", region.identifier, [NSDate date]);
}


(void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region {
    NSLog(@"didExitRegion %@ at %@", region.identifier, [NSDate date]);
}

(void)locationManager:(CLLocationManager *)manager monitoringDidFailForRegion:(CLRegion *)region withError:(NSError *)error {
    NSLog(@"monitoringDidFailForRegion %@: %@ Error: %@", region.identifier, [NSDate date], error);
}