2014-09-03 78 views
4

這裏是對於iOS健康工具包如何保存收縮壓和舒張壓值?

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) 

同樣的方式保存血壓數據在醫療包舒張也代碼,

但如何節省結合在衛生應用單次入境?目前在保健應用程序中爲收縮壓和舒張壓保存了兩個不同的條目。

回答

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

}]; 
} 
+0

很棒的回答。節省我很多麻煩 – sim 2014-09-18 06:22:45

+0

不錯的一個。當調用成功時,將更改UI的代碼放入'dispatch_async(dispatch_get_main_queue(),^ {})中,我將更改回主隊列線程。 ' – goldengil 2014-09-24 07:55:08

+0

任何人都有迅捷的版本? ;) – 2dvisio 2014-11-20 22:43:07

2

結賬HKCorrelation。相關性是一組相關對象,旨在表示血壓讀數和食物等。您可以像創建樣本一樣保存創建並保存關聯,並且您可以使用HKCorrelationQuery查詢關聯。

+0

任何指針的實例這兩個? – 2dvisio 2014-11-22 14:53:40

2

夫特: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 
1

Xamarin.iOS溶液

public void SaveBloodPressure(DateTime date, double systolic, double diastolic, double beatsPerMinute) 
    { 
     using (var healthKitStore = new HKHealthStore()) 
     { 
      var heartRateUnitType = HKUnit.MillimeterOfMercury; 
      var diastolicQuantity = HKQuantity.FromQuantity(heartRateUnitType, diastolic); 

      var diastolicQuantityType = HKQuantityType.GetQuantityType(HKQuantityTypeIdentifierKey.BloodPressureDiastolic); 
      var diastolicSample = HKQuantitySample.FromType(diastolicQuantityType, diastolicQuantity, date.ToUniversalTime().ToNSDate(), date.ToUniversalTime().ToNSDate(), new HKMetadata()); 


      var systolicQuantity = HKQuantity.FromQuantity(heartRateUnitType, systolic); 

      var systolicQuantityType = HKQuantityType.GetQuantityType(HKQuantityTypeIdentifierKey.BloodPressureSystolic); 
      var systolicSample = HKQuantitySample.FromType(systolicQuantityType, systolicQuantity, date.ToUniversalTime().ToNSDate(), date.ToUniversalTime().ToNSDate(), new HKMetadata()); 

      var objects = new NSSet(systolicSample, diastolicSample); 
      var bloodPressureType = HKCorrelationType.GetCorrelationType(HKCorrelationTypeKey.IdentifierBloodPressure); 
      var bloodPressure = HKCorrelation.Create(bloodPressureType, date.ToUniversalTime().ToNSDate(), date.ToUniversalTime().ToNSDate(), objects); 

      try 
      { 
       healthKitStore.SaveObject(bloodPressure, (success, error) => 
       {       
        //action to take on success/failure 
       }); 
      } 
      catch (Exception) 
      {     
       //handle exception 
      }    
      try 
      { 
       var beatsPerMinuteUnits = HKUnit.Count.UnitDividedBy(HKUnit.Minute); 
       var beatsPerMinuteQuantity = HKQuantity.FromQuantity(beatsPerMinuteUnits, beatsPerMinute); 

       var heartRateQuantityType = HKQuantityType.GetQuantityType(HKQuantityTypeIdentifierKey.HeartRate); 
       var heartRateSample = HKQuantitySample.FromType(heartRateQuantityType, beatsPerMinuteQuantity, date.ToUniversalTime().ToNSDate(), date.ToUniversalTime().ToNSDate(), new HKMetadata()); 
       healthKitStore.SaveObject(heartRateSample, (success, error) => 
       { 
        //handle success/failure 
       }); 
      } 
      catch (Exception) 
      { 
       //handle exception     
      }     
     } 
    } 
2

夫特3

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) 
    } 
}