如何读取healthKit心率数据? [英] How to read healthKit Heartrate data?
问题描述
我知道有人问过这个问题,但还没有真正回答.我已经尝试过像这样的线程做的事情: Apple的Healthkit带来的心率
I know this questions been asked, but hasn't really been answered. I've tried things from threads like this: Heart Rate With Apple's Healthkit
我尝试将其从Objective-C转换为Swift,但没有用.
I tried converting this from Objective-C to Swift but didn't work.
我的问题是,从保健套件中读取心率数据的最佳方法是什么.我希望能够从开始进行测量时就读取每个心率测量值,并且希望能够看到这些测量值的时间/日期戳.
My question is, what's the best way to read heart rate data from health kit. I want to be able to read every heart rate measurement from the time it started taking them, and I want to be able to see the time/day stamps of said measurements.
我在这里请求许可:
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 }
}
}
这是我现在的视图控制器代码(我不确定为什么这不起作用):
Here is my view Controller code for now (I'm not sure why this doesn't work):
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
}
}
我什么都无法打印到控制台,这确实是我想要的.
I can't get anything to print to the console, which is really just what I want.
此外-这些代码都没有用于家庭作业,个人/专业项目等.它只是出于娱乐/学习的目的,而这段代码大部分是我尝试过的,也是通过流和其他论坛中的多个堆栈查找得到的.
Also - none of this code is going towards homeworks, personal/professional projects...etc. Its just for fun/to learn, and most of this code is what I tried and what I've found looking through multiple stack over flow and other forums.
推荐答案
您需要实际执行查询.
let query = self.createStreamingQuery()
self.health.executeQuery(query)
这篇关于如何读取healthKit心率数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!