Skip to content

Commit 5cdd6eb

Browse files
authored
Merge pull request #280 from agencyenterprise/bugfix/crash-no-permission-statistics
Fix: Crash when getting statistical data without permissions
2 parents 195e3e4 + ff09ea8 commit 5cdd6eb

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.m

+5-5
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ - (void)fitness_getStepCountOnDay:(NSDictionary *)input callback:(RCTResponseSen
3535
includeManuallyAdded:includeManuallyAdded
3636
day:date
3737
completion:^(double value, NSDate *startDate, NSDate *endDate, NSError *error) {
38-
if (!value && value != 0) {
38+
if ((!value && value != 0) || error != nil) {
3939
callback(@[RCTJSErrorFromNSError(error)]);
4040
return;
4141
}
@@ -218,7 +218,7 @@ - (void)fitness_getDistanceWalkingRunningOnDay:(NSDictionary *)input callback:(R
218218
HKQuantityType *quantityType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDistanceWalkingRunning];
219219

220220
[self fetchSumOfSamplesOnDayForType:quantityType unit:unit includeManuallyAdded:includeManuallyAdded day:date completion:^(double distance, NSDate *startDate, NSDate *endDate, NSError *error) {
221-
if (!distance && distance != 0) {
221+
if ((!distance && distance != 0) || error != nil) {
222222
callback(@[RCTJSErrorFromNSError(error)]);
223223
return;
224224
}
@@ -277,7 +277,7 @@ - (void)fitness_getDistanceSwimmingOnDay:(NSDictionary *)input callback:(RCTResp
277277
HKQuantityType *quantityType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDistanceSwimming];
278278

279279
[self fetchSumOfSamplesOnDayForType:quantityType unit:unit includeManuallyAdded:includeManuallyAdded day:date completion:^(double distance, NSDate *startDate, NSDate *endDate, NSError *error) {
280-
if (!distance && distance != 0) {
280+
if ((!distance && distance != 0) || error != nil) {
281281
callback(@[RCTJSErrorFromNSError(error)]);
282282
return;
283283
}
@@ -334,7 +334,7 @@ - (void)fitness_getDistanceCyclingOnDay:(NSDictionary *)input callback:(RCTRespo
334334
HKQuantityType *quantityType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDistanceCycling];
335335

336336
[self fetchSumOfSamplesOnDayForType:quantityType unit:unit includeManuallyAdded:includeManuallyAdded day:date completion:^(double distance, NSDate *startDate, NSDate *endDate, NSError *error) {
337-
if (!distance && distance != 0) {
337+
if ((!distance && distance != 0) || error != nil) {
338338
callback(@[RCTJSErrorFromNSError(error)]);
339339
return;
340340
}
@@ -391,7 +391,7 @@ - (void)fitness_getFlightsClimbedOnDay:(NSDictionary *)input callback:(RCTRespon
391391
HKQuantityType *quantityType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierFlightsClimbed];
392392

393393
[self fetchSumOfSamplesOnDayForType:quantityType unit:unit includeManuallyAdded:includeManuallyAdded day:date completion:^(double count, NSDate *startDate, NSDate *endDate, NSError *error) {
394-
if (!count && count != 0) {
394+
if ((!count && count != 0) || error != nil) {
395395
callback(@[RCTJSErrorFromNSError(error)]);
396396
return;
397397
}

RCTAppleHealthKit/RCTAppleHealthKit+Queries.m

+6
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,12 @@ - (void)fetchSumOfSamplesOnDayForType:(HKQuantityType *)quantityType
756756
NSDate *startDate = result.startDate;
757757
NSDate *endDate = result.endDate;
758758
double value = [sum doubleValueForUnit:unit];
759+
if (startDate == nil || endDate == nil) {
760+
error = [[NSError alloc] initWithDomain:@"AppleHealthKit"
761+
code:0
762+
userInfo:@{@"Error reason": @"Could not fetch statistics: Not authorized"}
763+
];
764+
}
759765
completionHandler(value, startDate, endDate, error);
760766
}
761767
}];

0 commit comments

Comments
 (0)