如何在iOS中的健康工具包应用程序中保存血压数据

时间:2016-04-06 09:16:06

标签: ios objective-c iphone health-kit

以下是健康套件中的保存血压数据。

- (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}.

1 个答案:

答案 0 :(得分:2)

您应首先向用户请求权限:

    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]; 
                }
            }
        }];