Unable to Request Health Kit Permissions

I am working on a watch app which requests permissions to read and write active calorie data. My app works fine on Xcode 8 and watchOS3 + iOS 10, but on Xcode 9 and watchOS4 + iOS 11, I am unable request health kit permissions.


When I call requestAuthorization on HKHealthStore I get back success = false and the following error:

Error Domain=com.apple.healthkit Code=5 "Transaction block failed without an error." UserInfo={NSLocalizedDescription=Transaction block failed without an error.}

I also do not receive the alert on my phone to open my app and accept / decline the request for permissions.

My entitlements are setup correctly with com.apple.developer.healthkit set to YES for both the app and the watch extension. Since it worked fine when building to device on Xcode 8 and watchOS3 + iOS 10, but now no longer works on Xcode 9 and watchOS4 + iOS 11, my guess is that it's an SDK bug.

I encountered the same bug on the simulator, which I seemed to fix by erasing all content and settings, but I really don't want to have to do that on my actual device.

Any assistance from the Apple development team would be much appreciated. Since I never even get the alert on my phone, nor the alert on my watch to go to my phone to accept the permissions, my guess is that there's some bad state a device can get in where requestAuthorization on HKHealthStore just gets borked.

Replies

I encounter the same issue on watchOS 4 beta 4. Have you filed a bug to Apple yet?


Currently, I have to added a workaround to request the capability on iOS side in `func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool`.


So if this issue happens, users can open iOS counter app once and get the permission.

This issue is still occuring in the GM seed, fyi.

Same issue here with latest Xcode

** Update: This is not at all the issue. The solution I posted earlier is incorrect. I am sorry for it.

I agree with @georgbachmann and @htrhrth that "sometimes it works...sometimes it doesn't". Maybe something needs to be fixed from Apple's side.


As a workaround, and suggested by @zhaoxin, now my iOS app prompts for HealthKit permissions on launching the iPhone app.


I have had this issue earlier. Now got the crux of the problem and everything works correctly.

With Xcode 9 release version, one of my apps worked fine and correctly exhibited the HealthKit functionality with a prompt on Apple Watch and the UI on iPhone.

But another app of mine which accessed HealthKit behaved differently: it did show the prompt on Apple Watch and the Xcode displaye the following error:


Error Domain=com.apple.healthkit Code=5 "Transaction block failed without an error." UserInfo={NSLocalizedDescription=Transaction block failed without an error.}


So, I was doing something wrong with the second app. Analyzing the code, I found the problem: The second app was trying to access HealthKit data before the HealthkIt requestAuthorization(..) functionality was completed.


The requestAuthorization function displays a dialog and gives you the result in a background process. You have to wait for the answer of requestAuthorization before reading/writing HealthKit data.


So, I made sure that the code does not access HealthKit without authorization success and everything works fine now.



        let typestoRead = Set([
            HKObjectType.categoryType(forIdentifier: HKCategoryTypeIdentifier.mindfulSession)!,
            ])
  
        let typestoShare = Set([
            HKObjectType.categoryType(forIdentifier: HKCategoryTypeIdentifier.mindfulSession)!
            ])
  
        let healthStore = HKHealthStore()
        healthStore.requestAuthorization(toShare: typestoShare, read: typestoRead) { (success, error) -> Void in
            if(success){
                    // Read or write the HealthKit data
            }
            else{
                    // Authorization failure
            }    
        }


TLDR

Make sure you do not access HealthKit data before authorization success anywhere in your code.

I found the problem with on of my apps. Check my reply to hunhu. Hope it helps.

Same issue ... does anybody have a fix? @mithils solution doesn't work for me, cause I am not accessing HealthKit before the success block comes back...


I already figured out that it doesn't happen always... If I reinstall the app it sometimes works, but then most times it doesn't...

going to have to agree with oyu...sometimes it works...sometimes it doesn't. @mithils your code is in 'willActivate' correct?

@mithil - any other suggestions - sometimes i see the privacy screen - and sometimes i dont.

Yesterday everthing seemed to be working and today I checked, it does not! 😮 😕 The solution which I posted earlier is not at all the issue. Sorry for it guys.


I tried this:

1) Restarting Apple Watch

This resolved the problem and app worked normally.


2) Uninstall and reinstalling the app

This also solved the problem.


So, what I also conclude is: "Sometimes it works, and other times it does not"


I think something needs to get fixed from Apple's side.


@htrhrth: I call it from awake(withContext..). I assume that should not make a difference. Will it?

completely agree - “it sometimes works”. I would like to note the applicationShouldRequestHealthAuthorization method in the parent iOS appdelegate never gets called when the issue is happening. Not sure if this is the source of the problem (maybe there is an interop issue with the new versions). Regardless i submitted an app for review so I will check back wih the results.

Working on an app and came accross this exact issue.


If anyone has any update, I'd much appreciate hearing it. I'll try to update if I find any fix.


"Sometimes it works and sometimes it doesn't" can't be our only option? Users are going to slay my app in the reviews when they (rightly) complain how buggy it is!

Alright, I can confirm that this happens ~50% of the time on iOS 11.1 & WatchOS 4.1. I even tried adding error retries and 1 second delays but it would still fail.


Also I can confirm that this seems to work 100% of the time on iOS 10.3 and WatchOS 3.2.


Repo can be found, here:

https://github.com/adam-hanna/iOS11_watchOS4_failing_requestAuthorization

Bug report, here:
https://bugreport.apple.com/web/?problemID=35651796