如何读取HealthKit心率数据?

问题描述 投票:0回答:1

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

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

我的问题是,从 HealthKit 读取心率数据的最佳方式是什么。我希望能够读取从开始测量起的每一次心率测量值,并且我希望能够看到时间/日期标记说测量。

我在这里请求许可:

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
}


}

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

ios swift healthkit health-monitoring
1个回答
0
投票

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

let query = self.createStreamingQuery()
self.health.executeQuery(query)
© www.soinside.com 2019 - 2024. All rights reserved.