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.
IMHO: When the Block gets executed the object altimeter is already wiped by ARC. Try to make altimeter a property and it will work.
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)")
}
})
}
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 "
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With