(//..mm file fucntion)
- (void)requestPermissions:(NSArray *)readTypes
writeTypes:(NSArray *)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);
}
}
}];
}
< /code>
и назвал бы его с фронта.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)
}
< /code>
Однако это всегда дает ошибку: < /p>
FAILED prompting authorization request to share (null), read (
HKQuantityTypeIdentifierHeartRate
), error Required authorization not granted
< /code>
Перед тем, как вы спросите: < /p>
- Да, я добавил nshealthrequiredReadAuthorizationtypeidentifiers, nshealthShareUseScription, nshealthupdateUsagedescription и nshealthclinicalHealthRecordSshareUsageDescription к info.plist < /li> yeseallicealtheRecordsShareUseScription. com.apple.developer.healthkit, com.apple.developer.healthkit.access, com.apple.developer.healthkit.background-leviefy и com.apple.developer.healthkit.recalarate-espimates к идентификации и идентификации и идентификации и идентификации и идентификации и идентификации и идентификации. Xcode в разделе «Настройки сборки»
- Да, я добавил структуру HealthKit.
Подробнее здесь: https://stackoverflow.com/questions/796 ... n-not-gran