Healthstore read only in objective-C ?

I have an App in objective-c that is using Health data (walk/run, cycling) to give advice to users . I do not want/need to write any data in the Healtkit.

If i do (with the 3 values in the plist / .info :

self.healthStore requestAuthorizationToShareTypes:nil readTypes:readDataTypes 

My request crashes.

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Must request authorization for at least one data type' *** First throw call stack: ( 0 CoreFoundation 0x00000001804b910c __exceptionPreprocess + 172 1 libobjc.A.dylib 0x0000000180092da8 objc_exception_throw + 72 2 CoreFoundation 0x00000001804b901c -[NSException initWithCoder:] + 0 3 HealthKit 0x000000019da034d4 -[HKHealthStore _validateAuthorizationRequestWithShareTypes:readTypes:] + 92 4 HealthKit 0x000000019da03670 -[HKHealthStore requestAuthorizationToShareTypes:readTypes:shouldPrompt:completion:] + 292

BUT in swift :

healthStore.requestAuthorization(toShare: nil, read: readTypes)

is working, présents only my 2 datas to read... in the same IOS , same phone without crashing. What is the difference ?

Nil object in objective-c and Nil object in swift are not the same ? how do i make readonly requests in objective C ?

Answered by DTS Engineer in 828498022

I've tried the following code using Xcode 16.2 (16C5032a) + iOS 18.3.1, and confirmed the issue doesn't happen to me:

NSSet *readTypes = [NSSet setWithObject:[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount]];
[healthKitStore requestAuthorizationToShareTypes:nil readTypes:readTypes completion:^(BOOL success, NSError * _Nullable error) {
NSLog(@"%@", error);
}];

I did see the same crash if readTypes is nil or an empty set, which makes sense. If you try the same thing and the crash is still there, I'd be interested in looking into your project.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

Accepted Answer

I've tried the following code using Xcode 16.2 (16C5032a) + iOS 18.3.1, and confirmed the issue doesn't happen to me:

NSSet *readTypes = [NSSet setWithObject:[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount]];
[healthKitStore requestAuthorizationToShareTypes:nil readTypes:readTypes completion:^(BOOL success, NSError * _Nullable error) {
NSLog(@"%@", error);
}];

I did see the same crash if readTypes is nil or an empty set, which makes sense. If you try the same thing and the crash is still there, I'd be interested in looking into your project.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

My Init code is simple : if I Set like This I have a crash.

- (void) init_healthkit {
ALERTE_HEALTH = @"" ;
self.healthStore = [[HKHealthStore alloc] init];
if ([HKHealthStore isHealthDataAvailable]) {
NSLog(@"Healthkit available ");
NSSet *writeDataTypes = [self dataTypesToWrite];
NSSet *readDataTypes = [self dataTypesToRead];
[self.healthStore requestAuthorizationToShareTypes:nil readTypes:readDataTypes completion:^(BOOL success, NSError *error) {
if (!success) {
NSLog(@"You didn't allow HealthKit to access these read data types. In your app, try to handle this error gracefully when a user decides not to provide access. The error was: %@. If you're using a simulator, try it on a device.", error);
HEALTHKIT =@"N" ;
return;
}
NSLog(@"Healthkit OK read/write ");
dispatch_async(dispatch_get_main_queue(), ^{
// Update the user interface based on the current user's health information.
NSLog(@"Healthkit OK");
HEALTHKIT = @"Y" ;
});
}];
}
}

The code doesn't seem different from mine, except that it doesn't show what [self dataTypesToRead] is...

Do you try with the same testing environment I mentioned, which is Xcode 16.2 (16C5032a) + iOS 18.3.1? If yes and the crash still happens, maybe you can provide a minimal project for me to reproduce the issue. Your post can contain a link to where your test project is hosted.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

they are initialized :

  • (NSSet *)dataTypesToRead {

    HKCharacteristicType *distanceWalkingRunning = [HKObjectType characteristicTypeForIdentifier:HKQuantityTypeIdentifierDistanceWalkingRunning] ; HKCharacteristicType *StepCount = [HKSampleType characteristicTypeForIdentifier:HKQuantityTypeIdentifierStepCount]; HKCharacteristicType *cycleCount = [HKSampleType characteristicTypeForIdentifier:HKQuantityTypeIdentifierDistanceCycling]; return [NSSet setWithObjects: distanceWalkingRunning, StepCount,cycleCount , nil];

}

Written by DTS Engineer in 828498022
NSSet *readTypes = [NSSet setWithObject:[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount]]; [healthKitStore requestAuthorizationToShareTypes:nil readTypes:readTypes completion:^(BOOL success, NSError * _Nullable error) { NSLog(@"%@", error); }];

It's working. It ask for Steps and nothing else . I dont know what was bad , but tank you !

Healthstore read only in objective-C ?
 
 
Q