Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 900
abhishekkharwar Avatar asked Sep 29 '14 06:09

abhishekkharwar


3 Answers

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

like image 142
Thomson Avatar answered Nov 15 '22 01:11

Thomson


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)")
        }
    })
}
like image 41
Oleg Gordiichuk Avatar answered Nov 14 '22 23:11

Oleg Gordiichuk


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 "
            }
        }

    }
like image 24
AB Murphy Avatar answered Nov 14 '22 23:11

AB Murphy