I'm trying to collect health data in my Apple Watch application but when I call healthStore.requestAuthorization my app always crashes. I have set NSHealthShareUsageDescription and NSHealthUpdateUsageDescription in Info.plist as required but it still crashes. I started a new project and am just trying to request authorisation, there is no other functionality that could affect it. Any ideas why this is? Thanks for any help.
Replies
I was having the same issue using SwiftUI. But building a new project, using storyboards, revealed that the error was with my Info.plist strings. Apparently just a word or two is insuffcient.
Hopefully we'll get error messages passed through in SwiftUI soon.
Using longer strings solved my issue. I really hope this helps.
Cheers.
Thanks for the response. This still hasn't fixed the issue for me though. I'm using storyboards and my Info.plist strings are a length that should be suitable. No error is printed to the console so I have no reference as to what the problem could be.
I'm also experiencong the same issue - until Xcode 11.3, crash was happening without error message. With Xcode 11.4, the error displayed is:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'NSHealthUpdateUsageDescription must be set in the app's Info.plist in order to request write authorization for the following types: HKQuantityTypeIdentifierDietaryWater'
The string `NSHealthUpdateUsageDescription` is correctly set, along with `NSHealthShareUsageDescription`.
Stack trace:
*** First throw call stack:
(
0 CoreFoundation 0x04d86f52 __exceptionPreprocess + 370
1 libobjc.A.dylib 0x000d5dfd objc_exception_throw + 49
2 CoreFoundation 0x04d86d5f +[NSException raise:format:] + 95
3 HealthKit 0x004e217e -[HKHealthStore _validateHealthDataPurposeStringsForSharingTypes:readingTypes:isResearchStudy:] + 583
4 HealthKit 0x004e1be9 -[HKHealthStore _validatePurposeStringsForSharingTypes:readingTypes:] + 422
5 HealthKit 0x004e137e -[HKHealthStore requestAuthorizationToShareTypes:readTypes:shouldPrompt:completion:] + 384
6 HealthKit 0x004e10cd -[HKHealthStore requestAuthorizationToShareTypes:readTypes:completion:] + 72
Turns out the permissions strings need to be set on the Info.plist that lives in the `WatchKit Extension` directory, not the `Watchkit App` one. When set there, the app correctly prompts for the Health permissions.