Accessing Barometer to get relative altitude
Import the Core Motion library:
Next, we need to create a
CMAltimeter object, but a common pitfall is to create it in the
viewDidLoad(). If done that way, the altimeter won’t be accessible when we need to call a method on it. Nevertheless, go ahead and create your
CMAltimeter object just before the
We need to check if
relativeAltitudeis even available with the following method:
If that returns
true, you can then begin monitoring altitude change with
If there are no errors, you should be able to retrieve data from the
relativeAltitudeand pressure properties.
Given below is the definition of a button action to begin monitoring with our barometer.