[英]How to get the calories and heart rate from Health Kit Sdk in Swift?
I am creating an Fitness health app in which, I have to show user real time calories and their heart rate from Health kit. 我正在创建一个健身健康应用程序,其中,我必须显示用户实时卡路里和他们的心率从健康工具包。 I get the permissions.
我获得了权限。 But I don't know how to fetch the calories and heart rate.
但我不知道如何获取卡路里和心率。
Here is the permission of code. 这是代码的许可。
static let healthKitStore = HKHealthStore()
// MARK:- Health Kit Permissions :-
static func authorizeHealthKit() {
// HKQuantityTypeIdentifierBodyMass - For Weight
var shareTypes = Set<HKSampleType>()
shareTypes.insert(HKSampleType.workoutType())
shareTypes.insert(HKSampleType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeartRate)!)
shareTypes.insert(HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierDistanceWalkingRunning)!)
shareTypes.insert(HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierActiveEnergyBurned)!)
var readTypes = Set<HKObjectType>()
readTypes.insert(HKObjectType.workoutType())
readTypes.insert(HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeartRate)!)
readTypes.insert(HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierDistanceWalkingRunning)!)
readTypes.insert(HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierActiveEnergyBurned)!)
healthKitStore.requestAuthorizationToShareTypes(shareTypes, readTypes: readTypes) { (success, error) -> Void in
if success {
print("success")
} else {
print("failure")
}
if let error = error {
print(error)
}
}
}
After getting success I need the calories and heart rate which function or which code I have to use. 获得成功后,我需要卡路里和心率功能或我必须使用的代码。 Like Fitbit Apis return the data or there is Apple Api's which will return the data?
像Fitbit Apis一样返回数据还是Apple Api会返回数据?
Check this code: 检查此代码:
let tHeartRate = HKSampleType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeartRate)
let tHeartRateQuery = HKSampleQuery(sampleType: tHeartRate!, predicate:.None, limit: 0, sortDescriptors: nil) { query, results, error in
if results?.count > 0
{
var string:String = ""
for result in results as! [HKQuantitySample]
{
let HeartRate = result.quantity
string = "\(HeartRate)"
print(string)
}
}
}
self.healthKitStore.executeQuery(tHeartRateQuery)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.