ios - 尝试从HealthKit商店转换心率时发生异常

标签 ios objective-c ios8 healthkit

我正在开发我的第一个iPhone应用程序:一个简单的应用程序,很好地显示了HealthKit的heartRate结果。我的第一步是将结果显示为原始文本。但是不幸的是,我在下面的行中遇到一个异常,告诉我:“线程1信号SIGABRT”。有人知道,我做错了什么,并向我暗示了一个方向吗?

double usersBeatsPerMinute = [quantity doubleValueForUnit:[HKUnit countUnit]];

其余代码如下所示:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
// Set up an HKHealthStore, asking the user for read/write permissions. The profile view controller is the
// first view controller that's shown to the user, so we'll ask for all of the desired HealthKit permissions now.
// In your own app, you should consider requesting permissions the first time a user wants to interact with
// HealthKit data.
if ([HKHealthStore isHealthDataAvailable]) {
    NSSet *writeDataTypes = [self dataTypesToWrite];
    NSSet *readDataTypes = [self dataTypesToRead];

    [self.healthStore requestAuthorizationToShareTypes:writeDataTypes readTypes:readDataTypes completion:^(BOOL success, NSError *error) {
        if (!success) {
            NSLog(@"You didn't allow HealthKit to access these read/write data types. In your app, try to handle this error gracefully when a user decides not to provide access. The error was: %@. If you're using a simulator, try it on a device.", error);

            return;
        }

    }];
}

HKQuantityType *weightType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeartRate];

// Since we are interested in retrieving the user's latest sample
// we sort the samples in descending order by end date
// and set the limit to 1
// We are not filtering the data, and so the predicate is set to nil.
NSSortDescriptor *timeSortDescriptor = [[NSSortDescriptor alloc] initWithKey:HKSampleSortIdentifierEndDate ascending:NO];

// construct the query & since we are not filtering the data the predicate is set to nil
HKSampleQuery *query = [[HKSampleQuery alloc] initWithSampleType:weightType predicate:nil limit:1 sortDescriptors:@[timeSortDescriptor] resultsHandler:^(HKSampleQuery *query, NSArray *results, NSError *error) {

    // if there is a data point, dispatch to the main queue
    if (results) {
        dispatch_async(dispatch_get_main_queue(), ^{
            HKQuantitySample *quantitySample = results.firstObject;

            // pull out the quantity from the sample
            HKQuantity *quantity = quantitySample.quantity;

            double usersBeatsPerMinute = [quantity doubleValueForUnit:[HKUnit countUnit]];
            _HeartRateResults.text = [NSString stringWithFormat:@"%@ lbs", [NSNumberFormatter localizedStringFromNumber:@(usersBeatsPerMinute) numberStyle:NSNumberFormatterNoStyle]];
        });
    }
}];

// do not forget to execute the query after its constructed
[_healthStore executeQuery:query];}

最佳答案

我不太了解documentation中的注释(“这些样本使用计数/时间单位”),因此我进行了一些搜索并进行了尝试,并获得了使用以下方法手动输入到Health应用程序中的值:这个:

double rate = [mostRecentQuantity doubleValueForUnit:[[HKUnit countUnit] unitDividedByUnit:[HKUnit minuteUnit]]];

我以前没看过unitDividedByUnit。这是我从中拉出的the article

关于ios - 尝试从HealthKit商店转换心率时发生异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30867867/

相关文章:

ios - presentViewController 在 IOS 8 中闪烁

iphone - 声音输出含义

iphone - 如何只运行一次代码?

ios - 以编程方式更改 SCNScene 的 sceneNamed

ios持久化存储框架

Objective-C 简单的 hello world 无法编译

swift - 无论我做什么,Swift 中的 ABPersonH​​asImageData 总是返回 false

ios8 - AVPlayerItem videoComposition 卡住IOS8

ios - 在 TableView 中显示文本

ios - 使用的是哪个手势识别器?