WidgetKit + HealthKit Authorization

Let me start by saying, it wouldn't surprise me if all of the following is user error. 🙃

I'm trying to write a Widget using WidgetKit, where the widget extension itself is querying the HealthKit database. The host app definitely has HealthKit authorization, but I'm not clear how to do the same for the extension.

I did add the requisite Info.plist entry in the widget extension, which cleared my first error. However, when I do my normal prompt for auth — the same one I use in my main app — I see this in the console:

Code Block
2020-07-10 14:13:39.035757-0400 BodyWidgetExtension[988:337871] [auth] FAILED prompting authorization request to share (
), read (
    HKQuantityTypeIdentifierStepCount
)


This leads me to think this isn't my fault, but, perhaps I've missed the plot.

Any pointers/experience from anyone that's done this before?

Thank you!
Answered by Frameworks Engineer in 620397022
Hi!

Widgets can get access to health data if the host app has permission (which I believe is the case here). However, it doesn't have the ability to request authorization (show the health permission screen).

The best practice here is to never call requestAuthorization in your widget code, and only determine the authorization status of your request through getRequestStatusForAuthorization API, then either display the health data or ask the user to grant permission in app depending on the result.

If I've misunderstood anything please let me know.
Update:
Digging a little deeper, I now see more information about the error. This is... disheartening.

Code Block
Error Domain=com.apple.healthkit Code=111 "Unable to prompt for authorization using this type of extension; ignoring request." UserInfo={NSLocalizedDescription=Unable to prompt for authorization using this type of extension; ignoring request.}


Is there any way to read HealthKit data from within a Widget?
This has been filed as feedback FB7907616.
Accepted Answer
Hi!

Widgets can get access to health data if the host app has permission (which I believe is the case here). However, it doesn't have the ability to request authorization (show the health permission screen).

The best practice here is to never call requestAuthorization in your widget code, and only determine the authorization status of your request through getRequestStatusForAuthorization API, then either display the health data or ask the user to grant permission in app depending on the result.

If I've misunderstood anything please let me know.
That makes a lot of sense and got me straightened out; thank you!
WidgetKit + HealthKit Authorization
 
 
Q