-1

Here is the saving blood pressure data in 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];
  }];

}

If i run my application i got following exception in abort(); function

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}.
StefanS
  • 1,089
  • 1
  • 11
  • 38
satya
  • 241
  • 3
  • 11
  • Did you ask authorization to get access too HealthKit from user ? – Larme Apr 06 '16 at 09:19
  • @Larme thanks for your response. means what bro i can't understand.. actually in health kit application i can enable blood pressure in show on Dashboard – satya Apr 06 '16 at 09:22
  • It's pretty common when using different iOS frameworks to require permission from the user in order to proceed. I haven't read anything about HealthKit, but I'm sure they cover this. Have to read anything about HealtKit? I'd say no as you took the code from [here](http://stackoverflow.com/questions/25642949/for-ios-healthkit-how-to-save-systolic-and-diastolic-blood-pressure-values) and the dived straight in without reading anything, I'd wager... – trojanfoe Apr 06 '16 at 09:59
  • @daredevil have you any idea?? if you have any idea plz tell me – satya Apr 06 '16 at 10:06
  • try to add HKObjectType.workoutType() to healthStore.requestAuthorizationToShareTypes() hmm, this does not apply directly to you, found a code on https://forums.developer.apple.com/thread/20936 – StefanS Apr 06 '16 at 10:13
  • @daredevil still i can't find a solution.. will you solve my issue plz?? – satya Apr 06 '16 at 10:19

1 Answers1

2

You should request permission from user first:

    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]; 
                }
            }
        }];
s0urcer
  • 312
  • 2
  • 7
  • thanks for your response.. will you give me full code.. actually i struck this issue today morning onwards.. so plz give me full code..how to modify my code.. plz give me idea.. – satya Apr 06 '16 at 12:32
  • ya it is working good. but i want to pass values.. systolic and Diastolic values.. how can i pass values.. plz tell me – satya Apr 06 '16 at 12:50
  • @satya updated. But in production code you will need to remember if you've got response from the user and don't ask again if you did. – s0urcer Apr 06 '16 at 13:01
  • thanks for your repose.. but values are not inserted in health kit blood pressure systolic and Diastolic. still they are showing empty – satya Apr 06 '16 at 13:03
  • @satya do you have the same error or a new one? How do you know it's not inserted? – s0urcer Apr 06 '16 at 13:05
  • i did not get no error.. but i am check in health kit application after execution of the code. in health kit app i am going to blood pressure option.. they are not showing any values systolic and diastolic.. – satya Apr 06 '16 at 13:07
  • it is working bro thank u so much.. now there is no issues @s0urcer – satya Apr 06 '16 at 13:15