Search code examples
iosswiftsensors

Access pressure value from barometer in iOS


I would like to access the pressure value of the barometer on an iPhone using Swift. Can someone show me how to do that? I am using CLLocation to access the altitude. Can the same class be used for pressure?

edit:

I put the following in my viewDidLoad() method but it's not printing anything... Please advise.

let altimeter = CMAltimeter()
    if CMAltimeter.isRelativeAltitudeAvailable() {
        altimeter.startRelativeAltitudeUpdates(to: OperationQueue.current!, withHandler: { data, error in
            if !(error != nil) {
                print("Relative Altitude: \(data?.relativeAltitude)")
                print("Relative Pressure: \(data?.pressure)")
                self.pressLabel.text = String(describing: data?.pressure)
            }
        })
    }

Solution

  • Barometer readings have nothing at all to do with location.

    You need to look into the Core Motion library, specifically CMAltitude and CMAltitudeData. See How to use CMAltitudeHandler in Swift? for an example.