ios - 为 HKCategoryTypeIdentifierSleepAnalysis 查询 HealthKit

标签 ios swift nsdate healthkit

我已经构建了一个导休眠眠样本的方法,但我无法让它返回正确的 sleep 时间值。

查询 sleep 数据的方法如下所示:

func updateHealthCategories() {

    let categoryType = HKObjectType.categoryTypeForIdentifier(HKCategoryTypeIdentifierSleepAnalysis)

    let start = NSDate(dateString:"2015-11-04")
    let end = NSDate(dateString:"2015-11-05")

    let categorySample = HKCategorySample(type: categoryType!,
        value: HKCategoryValueSleepAnalysis.Asleep.rawValue,
        startDate: start,
        endDate: end)

    self.hoursSleep = Double(categorySample.value)

    print(categorySample.value)
}

日期格式如下:

extension NSDate
{
    convenience
    init(dateString:String) {
        let dateStringFormatter = NSDateFormatter()
        dateStringFormatter.dateFormat = "yyyy-MM-dd"
        dateStringFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
        let d = dateStringFormatter.dateFromString(dateString)!
        self.init(timeInterval:0, sinceDate:d)
    }
}

我正在调用 11 月 4 日至 5 日的数据,其中包含以下数据:

但是,categorySample.value 返回 1 而不是 3

最佳答案

您正在访问的值是类别样本值,HKCategoryType,而不是 sleep 小时数。

HKCategoryTypeIdentifierSleepAnalysis 的定义

typedef enum : NSInteger {
   HKCategoryValueSleepAnalysisInBed,
   HKCategoryValueSleepAnalysisAsleep,
} HKCategoryValueSleepAnalysis;

定义两个可能的值,0 或 1,其中 1 的值匹配 HKCategoryValueSleepAnalysisAsleep

获取 sleep 时间需要设置一个 HKSampleQuery

代码看起来像这样:

if let sleepType = HKObjectType.categoryTypeForIdentifier(HKCategoryTypeIdentifierSleepAnalysis) {

    let predicate = HKQuery.predicateForSamplesWithStartDate(startDate, endDate: endDate, options: .None)
    let sortDescriptor = NSSortDescriptor(key: HKSampleSortIdentifierEndDate, ascending: false)
    let query = HKSampleQuery(sampleType: sleepType, predicate: predicate, limit: 30, sortDescriptors: [sortDescriptor]) { (query, tmpResult, error) -> Void in                  
        if let result = tmpResult {
            for item in result {
                if let sample = item as? HKCategorySample {                     
                    let value = (sample.value == HKCategoryValueSleepAnalysis.InBed.rawValue) ? "InBed" : "Asleep"                     
                    print("sleep: \(sample.startDate) \(sample.endDate) - source: \(sample.source.name) - value: \(value)")
                    let seconds = sample.endDate.timeIntervalSinceDate(sample.startDate)
                    let minutes = seconds/60
                    let hours = minutes/60
                }
            }
        }
    }

    healthStore.executeQuery(query)
}

我从 http://benoitpasquier.fr/sleep-healthkit/ 中总结了这一点.

关于ios - 为 HKCategoryTypeIdentifierSleepAnalysis 查询 HealthKit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33557205/

相关文章:

ios - 弹出窗口中无法识别的选择器

ios - URLSession 的正确用法是什么,创建新的还是重用相同的

ios - 如何在 swift 4 中使用 json 对象发送 json 数据

swift - 'String' 不可转换为 'Int'

objective-c - 在任何时间点获取 CABasicAnimation 中扩展圆的大小

ios - 查看角半径问题

ios - XCode 6 GM 中的 Swift 编译错误

ios - 两个 UIViewController 之间的 UIPageViewController

iphone - 检查指定日期是今天、昨天还是将来的日期

ios - NSTimer 的奇怪行为