iOS传感器之气压计的使用

2/13/2017来源:iOS开发人气:436

气压计实际上叫测高仪,因为气压主要与海拔高度有关

气压计的硬件在iphpne内部,主要是检测大气压强(kPa)的,而大气压强由于海拔高度有关 不是检测风速的,不要以为对着iphone吹气,气压值就会变化

传感器不属于隐私功能,不需要授权

气压计是iPhone6开始加入的硬件,不支持6以前的机型

使用气压计相当简单

#import "ViewController.h" //传感器框架 #import <CoreMotion/CoreMotion.h> @interface ViewController () @PRoperty(nonatomic,strong)CMAltimeter *altimeter; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //1.创建气压计(测高仪),这里要用全局变量,不然会被释放 self.altimeter = [[CMAltimeter alloc] init]; //2.检测当前设备是否可用(iphone6机型之后新增) if([CMAltimeter isRelativeAltitudeAvailable]) { //3.开始检测气压 [self.altimeter startRelativeAltitudeUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMAltitudeData * _Nullable altitudeData, NSError * _Nullable error) { NSLog(@"高度:%0.2f m 气压值:%0.2f kPa",[altitudeData.relativeAltitude floatValue],[altitudeData.pressure floatValue]); }]; } // Do any additional setup after loading the view, typically from a nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end