title: Based on user current location Region Monitoring, Delete Monitored Region in iOS7
body: <p>I am working on such a project where apps do the following things :</p>
  
  <ol>
  <li>User select a radius (10 meter to 1000 meter) and go to next viewController by pressing "Go" button</li>
  <li>Here apps Grab users current position and start "region monitoring" based on that current position with selected radius</li>
  <li>If the user cross that certain boundary (10 meter to 1000 meter) then it gives an "ExitRegion" alert message. And start "region monitoring" again based on users new current position. And the apps keep doing this all the time, both foreground and background mode. I manage to do it &amp; it is working perfectly.</li>
  </ol>
  
  <p>But now for limitation of the number of region, monitoring by "Region Monitoring", i want to delete every single "monitored region", after it creates new one. So it should happen like :</p>
  
  <ul>
  <li>Start Region Monitoring based on users current position</li>
  <li>Exit certain region &amp; and get "Exit Region" alert message</li>
  <li>Delete this "Monitored Region" from <code>stopMonitoringForRegion</code> array</li>
  <li>Start again Region Monitoring based on users current position</li>
  <li>Exit certain region &amp; and get "Exit Region" alert message</li>
  <li>Delete this "Monitored Region" from <code>stopMonitoringForRegion</code> array</li>
  </ul>
  
  <p>And it should goes on like this. I am trying this but it is not working properly.</p>
  
  <p>Here is my code : </p>
  
  <pre><code>-(void)startLocationServices
  {
      if (self.locationManager == nil)
      {
          self.locationManager = [CLLocationManager new];
      }
      [self.locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
      [self.locationManager setDelegate:self];
      [self.locationManager setDistanceFilter:kCLDistanceFilterNone];
      //[self.locationManager startUpdatingLocation];
  }
  
  -(void) monitoringRegion
  {
      if (flag == 0)
      {
          if (flagForRemovingRegion == 1)
          {
              // Remove monitored region from "monitoredRegions" array after monitor 5 regions
              [self removingMonitoredRegion];
          }
  
          CLLocationCoordinate2D center = CLLocationCoordinate2DMake(locationManager.location.coordinate.latitude, locationManager.location.coordinate.longitude);
  
          CLCircularRegion *region = [[CLCircularRegion alloc] initWithCenter:center radius:myval identifier:@"Test"];
          CLLocationDegrees radius = myval;
  
          if (radius &gt; self.locationManager.maximumRegionMonitoringDistance)
          {
              radius = self.locationManager.maximumRegionMonitoringDistance;
          }
          [self.locationManager startMonitoringForRegion:region];
  
          flag = 1;
          flagForRemovingRegion = 1;
          self.availabilityTextView.text = [@"Your selected Radius:" stringByAppendingFormat:@"%i", self.myval];
      }
  }
  
  - (void)viewDidLoad
  {
      [super viewDidLoad];
      flag = 0;
      [self startLocationServices];
      [self monitoringRegion];
  }
  
  -(void) removingMonitoredRegion
  {
      [locationManager stopMonitoringForRegion:[[[locationManager monitoredRegions] allObjects] objectAtIndex:0]];
  
  
  }
  
  - (void)locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region
  {
  //    // regions are stored by system
      self.threeTextView.text = [@"Regions: \n\n" stringByAppendingFormat:@"%@", [[self.locationManager monitoredRegions] allObjects]];
  
      UIAlertView *alertViewOne = [[UIAlertView alloc] initWithTitle:@"Status" message:@"Region Monitoring started." delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
  
      [alertViewOne show];
  }
  
  - (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
  {
      UIAlertView *alertViewTwo = [[UIAlertView alloc] initWithTitle:@"Status" message:@"You Enter the region" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
  
      [alertViewTwo show];
      self.availabilityTextView.text = @"You enter the region!";
  }
  
  - (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
  {
      UIAlertView *alertViewThree = [[UIAlertView alloc] initWithTitle:@"Status" message:@"You Exit the region" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
  
      [alertViewThree show];
      flag = 0;
      self.availabilityTextView.text = @"You exit the region!";
      [self monitoringRegion];
  }
  
  - (void) locationManager:(CLLocationManager *)manager monitoringDidFailForRegion:(CLRegion *)region withError:(NSError *)error
  {
      self.availabilityTextView.text = [@"\nError:" stringByAppendingFormat:@"%@", [error localizedDescription]];
  }
  </code></pre>
  
  <p>I set <code>flagForRemovingRegion</code>, so that it will not try to delete the "Monitored Region" at the beginning of the apps. Because at the beginning it is NULL.
  If any one can understand my problem or have any kind of suggestion please reply back. 
  Thanks in Advanced. Have a nice day.</p>