如何使用Apple HealthKit获取每日睡眠时间?

时间:2015-06-08 05:56:04

标签: ios health-kit

我正在做一个应用程序,它可以读取Apple HealthKit的每日步骤和睡眠数据。

对于步骤,它非常简单,因为它是HKQuantityType,因此我可以在其上应用HKStatisticsOptionCumulativeSum选项。把开始日期,结束日期和日期间隔放入,你就明白了。

- (void)readDailyStepsSince:(NSDate *)date completion:(void (^)(NSArray *results, NSError *error))completion {
    NSDate *today = [NSDate date];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *comps = [calendar components:NSCalendarUnitDay|NSCalendarUnitMonth|NSCalendarUnitYear fromDate:date];
    comps.hour = 0;
    comps.minute = 0;
    comps.second = 0;

    NSDate *midnightOfStartDate = [calendar dateFromComponents:comps];
    NSDate *anchorDate = midnightOfStartDate;

    HKQuantityType *stepType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];
    HKStatisticsOptions sumOptions = HKStatisticsOptionCumulativeSum;
    NSPredicate *dateRangePred = [HKQuery predicateForSamplesWithStartDate:midnightOfStartDate endDate:today options:HKQueryOptionNone];

    NSDateComponents *interval = [[NSDateComponents alloc] init];
    interval.day = 1;
    HKStatisticsCollectionQuery *query = [[HKStatisticsCollectionQuery alloc] initWithQuantityType:stepType quantitySamplePredicate:dateRangePred options:sumOptions anchorDate:anchorDate intervalComponents:interval];

    query.initialResultsHandler = ^(HKStatisticsCollectionQuery *query, HKStatisticsCollection *result, NSError *error) {

        NSMutableArray *output = [NSMutableArray array];

        // we want "populated" statistics only, so we use result.statistics to iterate
        for (HKStatistics *sample in result.statistics) {
            double steps = [sample.sumQuantity doubleValueForUnit:[HKUnit countUnit]];
            NSDictionary *dict = @{@"date": sample.startDate, @"steps": @(steps)};
            //NSLog(@"[STEP] date:%@ steps:%.0f", s.startDate, steps);
            [output addObject:dict];
        }

        dispatch_async(dispatch_get_main_queue(), ^{
            if (completion != nil) {
                NSLog(@"[STEP] %@", output);
                completion(output, error);
            }
        });
    };

    [self.healthStore executeQuery:query];
}

但是对于睡眠,它并不是那么直接。我坚持了很多事情。

  • 首先,与步骤不同,睡眠是HKCategoryType。因此,我们无法使用HKStatisticsCollectionQuery对其进行求和,因为此方法仅接受HKQuantityType
  • 还有2种睡眠值,HKCategoryValueSleepAnalysisInBedHKCategoryValueSleepAnalysisAsleep。我不确定哪个值最适合睡眠持续时间。我只是为了简单起见而使用HKCategoryValueSleepAnalysisAsleep
  • 睡眠数据来自HKCategorySample个对象的数组。每个都有开始日期和结束日期。如何有效地组合这些数据,在一天之内将其修剪,并从中获取每日睡眠持续时间(以分钟为单位)?我在DateTool pod中找到了这个 DTTimePeriodCollection 类可以完成这项工作,但我还没弄明白。

简单地说,如果有人知道如何使用Apple HealthKit获得每日睡眠时间,请告诉我!

2 个答案:

答案 0 :(得分:0)

我用过这个:

@import HealthKit;

@implementation HKHealthStore (AAPLExtensions)


- (void)hkQueryExecute:(void (^)(double, NSError *))completion {
NSCalendar *calendar = [NSCalendar currentCalendar];

NSDate *now = [NSDate date];

NSDateComponents *components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:now];

NSDate *startDate = [calendar dateFromComponents:components];

NSDate *endDate = [calendar dateByAddingUnit:NSCalendarUnitDay value:1 toDate:startDate options:0];

HKSampleType *sampleType = [HKSampleType categoryTypeForIdentifier:HKCategoryTypeIdentifierSleepAnalysis];
NSPredicate *predicate = [HKQuery predicateForSamplesWithStartDate:startDate endDate:endDate options:HKQueryOptionNone];

HKSampleQuery *query = [[HKSampleQuery alloc] initWithSampleType:sampleType predicate:predicate limit:0 sortDescriptors:nil resultsHandler:^(HKSampleQuery *query, NSArray *results, NSError *error) {
    if (!results) {
        NSLog(@"An error occured fetching the user's sleep duration. In your app, try to handle this gracefully. The error was: %@.", error);
        completion(0, error);
        abort();
    }

        double minutesSleepAggr = 0;
        for (HKCategorySample *sample in results) {

            NSTimeInterval distanceBetweenDates = [sample.endDate timeIntervalSinceDate:sample.startDate];
            double minutesInAnHour = 60;
            double minutesBetweenDates = distanceBetweenDates / minutesInAnHour;

            minutesSleepAggr += minutesBetweenDates;
        }
        completion(minutesSleepAggr, error);
}];

[self executeQuery:query];
}

然后在视图控制器中:

- (void)updateUsersSleepLabel {
[self.healthStore hkQueryExecute: ^(double minutes, NSError *error) {
    if (minutes == 0) {
        NSLog(@"Either an error occured fetching the user's sleep information or none has been stored yet.");

        dispatch_async(dispatch_get_main_queue(), ^{
            self.sleepDurationValueLabel.text = NSLocalizedString(@"Not available", nil);
        });
    }
    else {

        int hours = (int)minutes / 60;
        int minutesNew = (int)minutes - (hours*60);
        NSLog(@"hours slept: %ld:%ld", (long)hours, (long)minutesNew);

        dispatch_async(dispatch_get_main_queue(), ^{
            self.sleepDurationValueLabel.text = [NSString stringWithFormat:@"%d:%d", hours, minutesNew] ;
        });
    }


}];
}

答案 1 :(得分:0)

检查我是如何做到的,它对我的​​工作是如何收集睡眠数据

onCreateView()

这给出了入口插槽的阵列。