简体   繁体   English

CMAltimeter回调永远不会触发

[英]CMAltimeter callback never fires

Using my 6+ I've been trying to read the relative altitude and pressure using CoreMotion's new CMAltimeter. 使用我的6+我一直在尝试使用CoreMotion的新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. 我非常尴尬地以如此巨大的疏忽来回答我自己的问题。

In the original post I had the CMAltimiter declared in the scope of viewDidLoad, thus it goes out of scope and is deallocated. 在原始帖子中,我在viewDidLoad的范围内声明了CMAltimiter,因此它超出了范围并被取消分配。 I moved it to be an iVar and the callback now fires. 我把它变成了iVar,现在回调了。

#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");
    }
}

You need to call [altimeterManager stopRelativeAltitudeUpdates]; 你需要调用[altimeterManager stopRelativeAltitudeUpdates]; for the references to be released to the dispatch queue. 用于将引用释放到调度队列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM