7

これは、ヘルスキットに血圧データを保存するためのコードです

 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) 

拡張期についても同じように、

しかし、健康アプリで両方を組み合わせて単一のエントリとして保存するにはどうすればよいですか? 現在、健康アプリには収縮期血圧と拡張期血圧の 2 つの異なるエントリが保存されています。

4

5 に答える 5

11
- (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];

}];
}
于 2014-09-15T13:07:45.293 に答える
3

Swift : iOS : 血圧を保存:

private func saveBloodPressureIntoHealthStore(bloodPressureValueSystolic:Double
        ,bloodPressureValueDiastolic:Double) -> Void {

            // Save the user's blood pressure into HealthKit.
            let bloodPressureUnit: HKUnit = HKUnit.millimeterOfMercuryUnit()

            let bloodPressureSystolicQuantity: HKQuantity = HKQuantity(unit: bloodPressureUnit, doubleValue: bloodPressureValueSystolic)

            let bloodPressureDiastolicQuantity: HKQuantity = HKQuantity(unit: bloodPressureUnit, doubleValue: bloodPressureValueDiastolic)

            let bloodPressureSystolicType: HKQuantityType = HKQuantityType.quantityTypeForIdentifier(HKQuantityTypeIdentifierBloodPressureSystolic)

            let bloodPressureDiastolicType: HKQuantityType = HKQuantityType.quantityTypeForIdentifier(HKQuantityTypeIdentifierBloodPressureDiastolic)

            let nowDate: NSDate = NSDate()

            let bloodPressureSystolicSample: HKQuantitySample = HKQuantitySample(type: bloodPressureSystolicType
                , quantity: bloodPressureSystolicQuantity, startDate: nowDate, endDate: nowDate)

            let bloodPressureDiastolicSample: HKQuantitySample = HKQuantitySample(type: bloodPressureDiastolicType
                , quantity: bloodPressureDiastolicQuantity, startDate: nowDate, endDate: nowDate)

            let completion: ((Bool, NSError!) -> Void) = {
                (success, error) -> Void in

                if !success {
                    println("An error occured saving the Blood pressure sample \(bloodPressureSystolicSample). In your app, try to handle this gracefully. The error was: \(error).")

                    abort()
                }

            }// end completion

            var objects : NSSet = NSSet(objects: bloodPressureSystolicSample,bloodPressureDiastolicSample)

            var bloodPressureType: HKCorrelationType = HKObjectType.correlationTypeForIdentifier(HKCorrelationTypeIdentifierBloodPressure)

            var bloodPressureCorrelation : HKCorrelation = HKCorrelation(type: bloodPressureType, startDate: nowDate
                , endDate: nowDate, objects: objects)

            self.healthStore!.saveObject(bloodPressureCorrelation, withCompletion: completion)

    }// end saveBloodPressureIntoHealthStore
于 2014-12-03T09:54:16.333 に答える
3

HKCorrelationをチェックしてください。相関関係は、関連するオブジェクトのセットであり、血圧測定値や食品などを表すように設計されています。サンプルと同じように相関を作成して保存し、HKCorrelationQueryを使用して相関を照会できます。

于 2014-09-08T16:30:02.943 に答える