How to use the iPhone6 ​​barometer with the new APIs available in iOS 8?

I am looking for how to use the apis barometer, which is available in iOS 8 for iPhone6.

I used the following code

if([CMAltimeter isRelativeAltitudeAvailable]){
    CMAltimeter *altimeter = [[CMAltimeter alloc] init];
    [altimeter startRelativeAltitudeUpdatesToQueue:queue withHandler:^(CMAltitudeData *altitudeData, NSError *error) {

        if(error)
            [label setText:[NSString stringWithFormat:@"%@",error.localizedDescription]];
        else
            [label setText:[NSString stringWithFormat:@"%@",altitudeData.relativeAltitude]];

    }];
}
else{
    [label setText:@"That not iPhone 6 for sure ;)"];
}

But its not working without even returning any error value. It seems that the completion block is not working, but my label is not updating. I am testing it on my iPhone 6.

+4
source share
2 answers

IMHO: When the block is executed, the altimeter of the object is already destroyed by ARC. Try to make the altimeter property, and it will work.

+5
source

Swift . , .

let altimeter = CMAltimeter()
if CMAltimeter.isRelativeAltitudeAvailable() {
    altimeter.startRelativeAltitudeUpdatesToQueue(NSOperationQueue.mainQueue(), withHandler: { data, error in
        if !error {
            println("Relative Altitude: \(data.relativeAltitude)")
        }
    })
}
+1

All Articles