CMAltimeter detect take off and landing

167 views Asked by At

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;
}
1

There are 1 answers

2
GlennRay On

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.