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 ?
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.