首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >对于iOS健康工具包,如何保存收缩压和舒张压值?

对于iOS健康工具包,如何保存收缩压和舒张压值?
EN

Stack Overflow用户
提问于 2014-09-03 11:16:32
回答 5查看 4K关注 0票数 7

以下是在健康工具包中保存血压数据的代码

代码语言:javascript
复制
 HKUnit *BPunit = [HKUnit millimeterOfMercuryUnit];
 HKQuantity *BPSysQuantity = [HKQuantity quantityWithUnit:BPunit doubleValue:150.0];
 HKQuantityType *BPSysType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierBloodPressureSystolic];
 HKQuantitySample *BPSysSample = [HKQuantitySample quantitySampleWithType:BPSysType quantity:BpsysQuantity startDate:now endDate:now];
 [self.healthStore saveObject:BPSysSample withCompletion:^(BOOL success, NSError *error) 

舒张压也是一样,

但是,如何在健康应用程序中将两者合并为单一条目呢?目前,两个不同的条目保存在健康应用中的收缩压和舒张压。

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2014-09-15 13:07:45

代码语言:javascript
复制
- (void)saveBloodPressureIntoHealthStore:(double)Systolic Dysbp:(double)Diastolic {

HKUnit *BloodPressureUnit = [HKUnit millimeterOfMercuryUnit];

HKQuantity *SystolicQuantity = [HKQuantity quantityWithUnit:BloodPressureUnit doubleValue:Systolic];
HKQuantity *DiastolicQuantity = [HKQuantity quantityWithUnit:BloodPressureUnit doubleValue:Diastolic];

HKQuantityType *SystolicType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierBloodPressureSystolic];
HKQuantityType *DiastolicType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierBloodPressureDiastolic];

NSDate *now = [NSDate date];

HKQuantitySample *SystolicSample = [HKQuantitySample quantitySampleWithType:SystolicType quantity:SystolicQuantity startDate:now endDate:now];
HKQuantitySample *DiastolicSample = [HKQuantitySample quantitySampleWithType:DiastolicType quantity:DiastolicQuantity startDate:now endDate:now];

NSSet *objects=[NSSet setWithObjects:SystolicSample,DiastolicSample, nil];
HKCorrelationType *bloodPressureType = [HKObjectType correlationTypeForIdentifier:
                                 HKCorrelationTypeIdentifierBloodPressure];
HKCorrelation *BloodPressure = [HKCorrelation correlationWithType:bloodPressureType startDate:now endDate:now objects:objects];
                                [self.healthStore saveObject:BloodPressure withCompletion:^(BOOL success, NSError *error) {
    if (!success) {
        NSLog(@"An error occured saving the height sample %@. In your app, try to handle this gracefully. The error was: %@.", BloodPressure, error);
        abort();
    }
    [_activity stopAnimating];
    UIAlertView *savealert=[[UIAlertView alloc]initWithTitle:@"HealthDemo" message:@"Blood Pressure values has been saved to Health App" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [savealert show];

}];
}
票数 11
EN

Stack Overflow用户

发布于 2017-01-15 07:50:30

Swift 3

代码语言:javascript
复制
func saveBloodPressure(systolic systolicValue: Double, diastolic diastolicValue: Double, completion completionBlock: @escaping (Bool, Error?) -> Void) {
    let unit = HKUnit.millimeterOfMercury()

    let systolicQuantity = HKQuantity(unit: unit, doubleValue: systolicValue)
    let diastolicQuantity = HKQuantity(unit: unit, doubleValue: diastolicValue)

    let systolicType = HKQuantityType.quantityType(forIdentifier: .bloodPressureSystolic)!
    let diastolicType = HKQuantityType.quantityType(forIdentifier: .bloodPressureDiastolic)!

    let nowDate = Date()
    let systolicSample = HKQuantitySample(type: systolicType, quantity: systolicQuantity, start: nowDate, end: nowDate)
    let diastolicSample = HKQuantitySample(type: diastolicType, quantity: diastolicQuantity, start: nowDate, end: nowDate)

    let objects: Set<HKSample> = [systolicSample, diastolicSample]
    let type = HKObjectType.correlationType(forIdentifier: .bloodPressure)!
    let correlation = HKCorrelation(type: type, start: nowDate, end: nowDate, objects: objects)

    self.healthKitStore.save(correlation) { (success, error) -> Void in
        if !success {
            print("An error occured saving the Blood pressure sample \(systolicSample). In your app, try to handle this gracefully. The error was: \(error).")
        }
        completionBlock(success, error)
    }
}
票数 4
EN

Stack Overflow用户

发布于 2014-09-08 16:30:02

查看HKCorrelation.关联是一组相关的对象,被设计用来表示血压读数和食物。您可以像示例一样保存、创建和保存相关性,并且可以使用HKCorrelationQuery.查询相关性。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25642949

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档