5

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

I have used 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's not iPhone 6 for sure ;)"];
}

But its not working even not returning any error value. It seems like completion block is not working coz my label is not updating. I am testing it on my iPhone 6.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
abhishekkharwar
  • 3,529
  • 2
  • 18
  • 31
  • What is the value of `queue`? If it’s not the main queue (and it shouldn’t be), updating your UI from it isn’t going to work. – Noah Witherspoon Sep 29 '14 at 19:45
  • possible duplicate of [CMAltimeter callback never fires](http://stackoverflow.com/questions/26028186/cmaltimeter-callback-never-fires) – fishinear Oct 17 '14 at 18:03

3 Answers3

5

IMHO: When the Block gets executed the object altimeter is already wiped by ARC. Try to make altimeter a property and it will work.

Thomson
  • 181
  • 2
  • 12
1

Try to use Swift code example and see the results. I am using this code sniper and it works.

let altimeter = CMAltimeter()
if CMAltimeter.isRelativeAltitudeAvailable() {
    altimeter.startRelativeAltitudeUpdatesToQueue(NSOperationQueue.mainQueue(), withHandler: { data, error in
        if !error {
            println("Relative Altitude: \(data.relativeAltitude)")
        }
    })
}
Oleg Gordiichuk
  • 15,240
  • 7
  • 60
  • 100
1

The question is about Barometric Pressure. Here is Swift 4.2 code to retrieve and display the iPhone 6 pressure readings. Note: I converted the reading result to milibars.
Remember to make an entry in the Info.plist file for: Privacy - Motion Usage Description. hth

import UIKit
import CoreMotion

class ViewController: UIViewController
{
        @IBOutlet weak var pressureLabel: UILabel!

        var rawPressure = 999.99
        let altimeter = CMAltimeter()

        override func viewDidLoad()
        {
            super.viewDidLoad()
            getSensorData()
        }

        func getSensorData()
        {
            if CMAltimeter.isRelativeAltitudeAvailable()
            {
                altimeter.startRelativeAltitudeUpdates(to: OperationQueue.main)
                {
                    (data, error) in
                    if !(error != nil)
                    {
                        self.rawPressure = Double(truncating: (data?.pressure)!) * 10.00
                        self.pressureLabel.text = String(format: "%.0f", self.rawPressure)+" mb"
    //                    print("Relative Pressure: \(self.pressureLabel.text ?? "error: 02")")
                    } else {
                        self.pressureLabel.text = " Oops!  1 "
                    }
                }
            }  else {
                self.pressureLabel.text = " Oops!  2 "
            }
        }

    }
AB Murphy
  • 51
  • 4