【问题标题】:How to read healthKit Heartrate data?如何读取 healthKit 心率数据?
【发布时间】:2016-05-14 17:49:18
【问题描述】:

我知道有人问过这个问题,但还没有真正得到回答。我已经尝试过这样的线程: Heart Rate With Apple's Healthkit

我尝试将其从 Objective-C 转换为 Swift,但没有成功。

我的问题是,从 Health Kit 读取心率数据的最佳方法是什么。我希望能够从开始测量每一个心率测量值开始读取它们,并且我希望能够看到所述测量值的时间/日期标记。

我在这里请求许可:

import Foundation
import UIKit
import HealthKit

class HealthKitManager: NSObject {

static let healthKitStore = HKHealthStore()

static func authorizeHealthKit() {

    let healthKitTypes: Set = [
        HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeartRate)!,
    ]

    healthKitStore.requestAuthorizationToShareTypes(healthKitTypes,
                                                    readTypes: healthKitTypes) { _, _ in }
    }
}

这是我现在的视图控制器代码(我不确定为什么这不起作用):

import UIKit
import HealthKit

class ViewController: UIViewController {

let health: HKHealthStore = HKHealthStore()
let heartRateUnit:HKUnit = HKUnit(fromString: "count/min")
let heartRateType:HKQuantityType   = HKQuantityType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeartRate)!
var heartRateQuery:HKQuery?


override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

     @IBAction func authorizeTapped(sender: AnyObject) {
    print("button tapped")
    self.createStreamingQuery()
    HealthKitManager.authorizeHealthKit()

}


func createStreamingQuery() -> HKQuery
{
    let queryPredicate  = HKQuery.predicateForSamplesWithStartDate(NSDate(), endDate: nil, options: .None)

    let query:HKAnchoredObjectQuery = HKAnchoredObjectQuery(type: self.heartRateType, predicate: queryPredicate, anchor: nil, limit: Int(HKObjectQueryNoLimit))
    { (query:HKAnchoredObjectQuery, samples:[HKSample]?, deletedObjects:[HKDeletedObject]?, anchor:HKQueryAnchor?, error:NSError?) -> Void in

        if let errorFound:NSError = error
        {
            print("query error: \(errorFound.localizedDescription)")
        }
        else
        {
            //printing heart rate
            if let samples = samples as? [HKQuantitySample]
            {
                if let quantity = samples.last?.quantity
                {
                    print("\(quantity.doubleValueForUnit(self.heartRateUnit))")
                }
            }
        }
    }

    query.updateHandler =
        { (query:HKAnchoredObjectQuery, samples:[HKSample]?, deletedObjects:[HKDeletedObject]?, anchor:HKQueryAnchor?, error:NSError?) -> Void in

            if let errorFound:NSError = error
            {
                print("query-handler error : \(errorFound.localizedDescription)")
            }
            else
            {
                //printing heart rate
                if let samples = samples as? [HKQuantitySample]
                {
                    if let quantity = samples.last?.quantity
                    {
                        print("\(quantity.doubleValueForUnit(self.heartRateUnit))")
                    }
                }
            }//eo-non_error
    }//eo-query-handler

    return query
}


}

我无法将任何内容打印到控制台,这正是我想要的。

另外 - 这些代码都不会用于家庭作业、个人/专业项目......等。它只是为了好玩/学习,大部分代码都是我尝试过的,也是我通过多个堆栈溢出和其他论坛发现的。

【问题讨论】:

  • 你有没有真正执行过你的createStreamingQuery函数?
  • 我做了,控制台没有打印任何东西,不知道为什么。
  • 你在健康存储上使用了 executeQuery 吗?
  • 我正在打印“
  • 您已经创建了一个查询,但您没有共享任何代码来证明您确实运行了查询

标签: ios swift healthkit health-monitoring


【解决方案1】:

您需要实际执行您的查询。

let query = self.createStreamingQuery()
self.health.executeQuery(query)

【讨论】:

  • 谢谢!还有一件事,现在正在打印内存地址,如何将其更改为可读数据?
  • @dnaland 对不起,我不明白你在说什么。你能澄清一下吗?
  • 当我尝试运行代码时,这是打印到控制台的内容: 警告:无法加载任何目标-C 类信息。这将显着降低可用类型信息的质量。如何将 转换为读取数据?
  • @dnaland 尝试在您正在打印的任何内容上调用 .description。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多