如何在 iOS 中将血压数据保存在 Health Kit 应用程序中

how to save the blood pressure data in health kit app in iOS

这里是健康包里保存的血压数据

- (void)viewDidLoad {
  Systolic  = 120;
  Diastolic = 90;

  [[GSHealthKitManager sharedManager]saveBloodPressureIntoHealthStore:Systolic Dysbp:Diastolic];
}

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

    UIAlertView *savealert=[[UIAlertView alloc]initWithTitle:@"HealthDemo" message:@"Blood Pressure values has been saved to Health App" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];

    [savealert show];
  }];

}

如果我 运行 我的应用程序在 abort() 中出现以下异常;函数

An error occurred saving the height sample <HKCorrelation>  2016-04-06 14:42:47 +0530 2016-04-06 14:42:47 +0530 (2 objects). In your app, try to handle this gracefully. The error was: Error Domain=com.apple.healthkit Code=5 "Authorization is not determined" UserInfo={NSLocalizedDescription=Authorization is not determined}.

您应该首先请求用户的许可:

    HKQuantityType* t1 = [HKQuantityType quantityTypeForIdentifier: HKQuantityTypeIdentifierBloodPressureSystolic],
    t2 = [HKQuantityType quantityTypeForIdentifier: HKQuantityTypeIdentifierBloodPressureDiastolic];

    NSSet * requestedTypesSet = [[NSSet alloc] initWithObjects: t1, t2, nil];

    [self.healthStore requestAuthorizationToShareTypes: requestedTypesSet readTypes:requestedTypesSet completion:^(BOOL success, NSError *error) {
        //user response processing goes here, i.e.
           if(success) { 
                dispatch_async(dispatch_get_main_queue(), ^{ 
                     [self saveBloodPressureIntoHealthStore:Systolic Dysbp:Diastolic]; 
                }
            }
        }];