CMAltimeter回调永远不会触发 [英] CMAltimeter callback never fires
问题描述
使用我的6+我一直试图使用CoreMotion的新CMAltimeter读取相对高度和压力。然而,回调永远不会被解雇。我有一个非常相似的设置,而是使用加速度计,陀螺仪和磁力计。他们似乎都很好。
Using my 6+ I've been trying to read the relative altitude and pressure using CoreMotion's new CMAltimeter. However the callback is never firing. I have a very similar setup which instead uses the accelerometers, gyros, and magnetometers. They all seem to work fine.
想知道是否有人能够获得阅读?
Was wondering if anyone out there has managed to get a reading?
- (void)viewDidLoad {
[super viewDidLoad];
if([CMAltimeter isRelativeAltitudeAvailable]){
CMAltimeter *altimeterManager = [[CMAltimeter alloc]init];
[altimeterManager startRelativeAltitudeUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMAltitudeData *altitudeData, NSError *error) {
// This never fires.
NSString *data = [NSString stringWithFormat:@"Altitude: %f %f", altitudeData.relativeAltitude.floatValue, altitudeData.pressure.floatValue];
NSLog(@"%@", data);
self.altimeterLabel.text = data;
}];
NSLog(@"Started altimeter");
self.altimeterLabel.text = @"-\n-";
} else {
NSLog(@"Altimeter not available");
}
}
我试过快走,但是这里只有一个高低失落的故事。
I've tried taking this on a quick walk, but there's only one story of altitude to lose/gain around here.
推荐答案
我非常尴尬地以如此巨大的疏忽来回答我自己的问题。
I'm pretty embarrased to answer my own question with such a huge oversight.
在原始帖子中,我在viewDidLoad的范围内声明了CMAltimiter,因此它超出了范围并被取消分配。我把它变成了iVar,现在回调了。
In the original post I had the CMAltimiter declared in the scope of viewDidLoad, thus it goes out of scope and is deallocated. I moved it to be an iVar and the callback now fires.
#import "ViewController.h"
@import CoreMotion;
@interface ViewController ()
@property (nonatomic, strong) CMAltimeter *altimeterManager;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
if([CMAltimeter isRelativeAltitudeAvailable]){
self.altimeterManager = [[CMAltimeter alloc]init];
[self.altimeterManager startRelativeAltitudeUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMAltitudeData *altitudeData, NSError *error) {
// This now fires properly
NSString *data = [NSString stringWithFormat:@"Altitude: %f %f", altitudeData.relativeAltitude.floatValue, altitudeData.pressure.floatValue];
NSLog(@"%@", data);
self.altimeterLabel.text = data;
}];
NSLog(@"Started altimeter");
self.altimeterLabel.text = @"-\n-";
} else {
NSLog(@"Altimeter not available");
}
}
这篇关于CMAltimeter回调永远不会触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!