[英]How to read iOS Healthkit Blood Pressure (systolic,diastolic) using HKObserverquery?
By using the following code i can read heart rate data automatically using observer query, when data is available in healthkit. 当Healthkit中的数据可用时,通过使用以下代码,我可以使用观察者查询自动读取心率数据。
HKSampleType *readGlucoseType; HKSampleType * readGlucoseType;
readGlucoseType = [HKSampleType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeartRate];
query = [[HKObserverQuery alloc]initWithSampleType:readGlucoseType
predicate:nil
updateHandler:^(HKObserverQuery *query, HKObserverQueryCompletionHandler completionHandler, NSError *error)
{
if (!error)
{
[self handleHKQueryResponse:query completionHandler:completionHandler errorCode:error];
}
else
{
NSLog(@"Observerquery Error");
if (completionHandler)
{
completionHandler();
}
}
}];
[self.healthStore executeQuery:query];
How do i need to read the blood pressure data from health kit using observer query? 我该如何使用观察者查询从保健套件中读取血压数据?
Updated the answer for Xcode 9.2: 更新了Xcode 9.2的答案:
func readSampleByBloodPressure()
{
guard let type = HKQuantityType.correlationType(forIdentifier: HKCorrelationTypeIdentifier.bloodPressure),
let systolicType = HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.bloodPressureSystolic),
let diastolicType = HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.bloodPressureDiastolic) else {
return
}
let sampleQuery = HKSampleQuery(sampleType: type, predicate: nil, limit: 0, sortDescriptors: nil) { (sampleQuery, results, error) in
if let dataList = results as? [HKCorrelation] {
for data in dataList
{
if let data1 = data.objects(for: systolicType).first as? HKQuantitySample,
let data2 = data.objects(for: diastolicType).first as? HKQuantitySample {
let value1 = data1.quantity.doubleValue(for: HKUnit.millimeterOfMercury())
let value2 = data2.quantity.doubleValue(for: HKUnit.millimeterOfMercury())
print("\(value1) / \(value2)")
}
}
}
}
healthStore.execute(sampleQuery)
}
Chances are that you came here looking for a solution how to read blood pressure from healthkit in swift. 您可能是来这里寻求解决方案的,该方法如何快速读取Healthkit的血压。 With that said, here is my solution in swift. 话虽如此,这是我的迅速解决方案。
func readSampleByBloodPressure()
{
let past = NSDate.distantPast() as! NSDate
let now = NSDate()
let sortDescriptor = NSSortDescriptor(key:HKSampleSortIdentifierStartDate, ascending: true)
let type = HKQuantityType.correlationTypeForIdentifier(HKCorrelationTypeIdentifierBloodPressure)
let sampleQuery = HKSampleQuery(sampleType: type, predicate: nil, limit: 0, sortDescriptors: [sortDescriptor])
{ (sampleQuery, results, error ) -> Void in
let dataLst = results as? [HKCorrelation];
for var index=0;index<dataLst!.count;++index
{
let data1 = (dataLst![index].objectsForType(HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierBloodPressureSystolic))).first as? HKQuantitySample
let data2 = dataLst![index].objectsForType(HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierBloodPressureDiastolic)).first as? HKQuantitySample
if let value1 = data1!.quantity?.doubleValueForUnit(HKUnit.millimeterOfMercuryUnit()) , value2 = data2!.quantity?.doubleValueForUnit(HKUnit.millimeterOfMercuryUnit()) {
println(value1)
println(value2)
}
}
}
self.healthKitStore.executeQuery(sampleQuery)
}
Here is the sample code for getting Blood pressure data from health kit. 这是从医疗工具包获取血压数据的示例代码。
HKCorrelationType *correlationType = [HKCorrelationType correlationTypeForIdentifier:HKCorrelationTypeIdentifierBloodPressure];
[self.healthStore enableBackgroundDeliveryForType:sampleType frequency:HKUpdateFrequencyImmediate withCompletion:^(BOOL success, NSError *error) {}];
HKObserverQuery *query = [[HKObserverQuery alloc] initWithSampleType:sampleType predicate:nil updateHandler:^(HKObserverQuery *query, HKObserverQueryCompletionHandler completionHandler, NSError *error) {
if (!error)
{
[self handleHKQueryResponse:query completionHandler:completionHandler errorCode:error];
}
else
{
NSLog(@"Observerquery Error");
if (completionHandler)
{
completionHandler();
}
}
[self.healthStore executeQuery:query];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.