氣壓計實際上叫測高儀,因為氣壓主要與海拔高度有關
傳感器不屬於隱私功能,不需要授權
氣壓計是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
本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。