I am trying to build a new Turbo native module in React-native to interact with the HealthKit but somehow I am stuck. I put together the appspec and it works, okay, then I went and tried to read the documentation and wrote the module like this:
(//..mm file fucntion)- (void)requestPermissions:(NSArray<NSString *> *)readTypes writeTypes:(NSArray<NSString *> *)writeTypes resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject { NSLog(@"RCTNativeHealthKit: Attempting minimal hardcoded permission request"); if (![HKHealthStore isHealthDataAvailable]) { reject(@"health_data_unavailable", @"HealthKit is not available on this device", nil); return; } NSMutableSet *typesToShare = [NSMutableSet set]; [typesToShare addObject:[HKObjectType workoutType]]; NSMutableSet *typesToRead = [NSMutableSet set]; for (NSString *typeString in readTypes) { if ([typeString isEqualToString:@"HeartRate"]) { HKQuantityType *heartRate = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeartRate]; if (heartRate) [typesToRead addObject:heartRate]; } } NSLog(@"RCTNativeHealthKit: Requesting with %lu share types and %lu read types", (unsigned long)typesToShare.count, (unsigned long)typesToRead.count); [self.healthStore requestAuthorizationToShareTypes:nil readTypes:typesToRead completion:^(BOOL success, NSError *_Nullable error) { NSLog(@"RCTNativeHealthKit: Callback with success: %@, error: %@", success ? @"YES" : @"NO", error ? error.localizedDescription : @"none"); if (success) { resolve(@(YES)); } else { if (error) { reject(@"permission_error", error.localizedDescription, error); } else { reject(@"permission_error", @"Unknown error occurred", nil); } } }];}
and would call it from the frontend something like this:
static async requestHealthPermissions() { try { console.log('Requesting health permissions for:', HEALTH_METRICS); const granted = await NativeHealthKit.requestPermissions("heartRate", // Read permissions [] // Write permissions (empty for now) ); console.log('Health permissions granted response:', granted); if (granted) { await Keychain.setGenericPassword( HEALTH_PERMISSIONS_KEY,'true', { service: HEALTH_PERMISSIONS_KEY } );//rest of the code}}catch(e){console.log(e)}
however this always gives the error:
FAILED prompting authorization request to share (null), read ( HKQuantityTypeIdentifierHeartRate), error Required authorization not granted
Before you ask:
- Yes I added NSHealthRequiredReadAuthorizationTypeIdentifiers, NSHealthShareUsageDescription, NSHealthUpdateUsageDescription and NSHealthClinicalHealthRecordsShareUsageDescription to the info.Plist
- yes I added com.apple.developer.healthkit, com.apple.developer.healthkit.access, com.apple.developer.healthkit.background-delivery and com.apple.developer.healthkit.recalibrate-estimates to the entitlements file
- yes I added the healthkit capabilities both to the identifier and in Xcode under build settings
- Yes I added the healthkit framework.
Many thanks already in advance 🙏.