-1
我正在Swift中爲IOS應用程序構建一個應用程序。我只想顯示最近保存的Healthkit的心率。有人可以幫我嗎?用swift顯示保存在Healthkit中的最後心率
我的授權請求:
static func authorizeHealthKit() {
let HeartRateData: Set = [
HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.heartRate)!,
]
HealthStore.requestAuthorization(toShare: HeartRateData, read: HeartRateData) {_,_ in }
}
@IBAction func authorizeTapped(_ sender: AnyObject) {
HeartrateViewController.authorizeHealthKit()
}
GET心臟率:
func fetchHeartRateWithCompletionHandler (_ completionHandler: @escaping (Double?, NSError?)->()) {
let now = Date()
let startDate = now
let endDate = now
let HeartRateSampleType = HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.heartRate)
let predicate = HKQuery.predicateForSamples(withStart: startDate, end: endDate, options: .strictStartDate)
let HeartRate = HKStatisticsQuery(quantityType: HeartRateSampleType!, quantitySamplePredicate: predicate, options: .cumulativeSum)
{ query, result, error in
if result != nil {
completionHandler(nil, error as NSError?)
return
}
var HeartRateData = 0.0
if let quantitiy = result!.sumQuantity() {
let heartRateUnit = HKUnit(from: "count/min")
HeartRateData = quantitiy.doubleValue(for: heartRateUnit)
}
completionHandler(HeartRateData, error as NSError?)
}
HeartRateTextField.text = String (describing: HeartRate)
}
,但它不會顯示在心臟率文本字段什麼。
至少嘗試編寫一些代碼並自己做一些研究。你搜索什麼?你嘗試了什麼?你遇到什麼問題? – Damien
請參閱此[post](http://stackoverflow.com/questions/37229992/how-to-read-healthkit-heartrate-data)。它應該幫助 –
我在我的問題中添加了我的代碼 –