@@ -15,17 +15,24 @@ @implementation RCTAppleHealthKit (Methods_Vitals)
15
15
- (void )vitals_saveHeartRateSample : (NSDictionary *)input callback : (RCTResponseSenderBlock)callback
16
16
{
17
17
NSDate *timeHeartRateSampleWasTaken = [RCTAppleHealthKit dateFromOptions: input key: @" date" withDefault: [NSDate date ]];
18
- double heartRateValue = [RCTAppleHealthKit doubleFromOptions: input key: @" value" withDefault: (double )60 ]; // Default HR is 60
19
-
18
+ double heartRateValue = [RCTAppleHealthKit doubleFromOptions: input key: @" value" withDefault: -99 ];
19
+ if (heartRateValue == -99 ){
20
+ callback (@[RCTMakeError (@" heartRateValue is required in options" , nil , nil )]);
21
+ return ;
22
+ }
23
+
24
+ HKUnit *count = [HKUnit countUnit ];
25
+ HKUnit *minute = [HKUnit minuteUnit ];
26
+ HKUnit *unit = [RCTAppleHealthKit hkUnitFromOptions: input key: @" unit" withDefault: [count unitDividedByUnit: minute]];
27
+
20
28
HKQuantitySample* heartRate = [HKQuantitySample quantitySampleWithType: [HKQuantityType quantityTypeForIdentifier: HKQuantityTypeIdentifierHeartRate]
21
- quantity: [HKQuantity quantityWithUnit: [HKUnit unitFromString: @" count/min " ] doubleValue: heartRateValue]
29
+ quantity: [HKQuantity quantityWithUnit: unit doubleValue: heartRateValue]
22
30
startDate: timeHeartRateSampleWasTaken
23
31
endDate: timeHeartRateSampleWasTaken];
24
32
25
- // Save the HeartRate Sample to HealthKit //
26
33
[self .healthStore saveObject: heartRate withCompletion: ^(BOOL success, NSError *error) {
27
34
if (!success) {
28
- NSLog (@" An error occured saving the heart rate sample %@ . The error was: " , error);
35
+ NSLog (@" An error occured saving the heart rate sample: %@ " , error);
29
36
callback (@[RCTMakeError (@" An error occured saving the heart rate sample" , error, nil )]);
30
37
return ;
31
38
}
0 commit comments