0

I would like to know how I can create a notification when the altitude is increasing and a notification when the altitude decreasing. I already tried this code but I've no idea what to do next.

- (CMAltimeter *)altimeter {
    if (!_altimeter) {
        _altimeter = [[CMAltimeter alloc]init];
    }
    if ([CMAltimeter isRelativeAltitudeAvailable]) {
        CMAltimeter* altimeter = [[CMAltimeter alloc] init];

        NSOperationQueue* queue = [[NSOperationQueue alloc] init];
        [altimeter startRelativeAltitudeUpdatesToQueue:queue withHandler:^(CMAltitudeData* altitudeData, NSError* error) {
        }];
    }
    return _altimeter;
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
pilou
  • 75
  • 1
  • 7
  • What you want to do is keep the last, say, 50 updates in a circular buffer and analyse the buffer every time an update occurs to see if there is a sudden increase or decrease. You would probably also need to compare the actual ground altitude or similar. – matt Mar 20 '18 at 23:05

1 Answers1

0

You pull out the data each time there's an update:

 [altimeter startRelativeAltitudeUpdatesToQueue:queue      
 withHandler:^(CMAltitudeData* altitudeData, NSError* error) 
 {
    // Put your data-handling code here --  for example, 
    // if your display has an element userRelAltitude 
    // that displays text:

    float relAltitude;
    relAltitude = altitudeData.relativeAltitude.floatValue;
    self.userRelAltitude.text = [NSString stringWithFormat:@"%.0f m", relAltitude];
  }];

Then you can compare each value to the previous one to see if it's increasing or decreasing and display an appropriate notification.

GlennRay
  • 959
  • 9
  • 18
  • Where I put your code ? In the same method or a other one ? – pilou Mar 21 '18 at 08:56
  • You could use the same method -- what's done between those curly brackets is done each time there is an update. You could put in code that adds the updated value to an array, and then, when you hit the 100th or 1000th value, set up the notification based on the value change throughout those last 100 (or 1000) values. – GlennRay Mar 22 '18 at 18:11