如何在Health kit应用程序中显示睡眠分析数据

时间:2016-04-14 08:07:08

标签: ios objective-c iphone health-kit

我有一个具有所有正确设置的HealthKit对象,我正在尝试写入HealthKit的睡眠分析数据区域,但我无法在HKCategoryTypeSleepAnalysis中找到该类型。  HKCategoryTypeSleepAnalysis有一些子项(即HKCategoryValueSleepAnalysisInBedHKCategoryValueSleepAnalysisAsleep)。这是我的代码:

- (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];

        for (HKStatistics *sample in result.statistics) {

            double steps = [sample.sumQuantity doubleValueForUnit:[HKUnit countUnit]];

            NSDictionary *dict = @{@"date": sample.startDate, @"steps": @(steps)};

            [output addObject:dict];

        }

        dispatch_async(dispatch_get_main_queue(), ^{

            if (completion != nil) {

                NSLog(@"[STEP] %@", output);

                completion(output, error);

            }

        });

    };

    [self.healthStore executeQuery:query];

}

我怀疑如何在Health应用程序中显示Sleep Analysis中的详细信息。实际上,睡眠分析中没有显示这些值。

0 个答案:

没有答案