Debugging Stories: Health Kit Entitlement Denied!

For the past week I have been working on a WatchOS application that allows you to track your calories and heart beat using your Apple Watch and the Health Kit App on your IPhone.

To do this I watched the Apple Developer video “What’s New in Health Kit” where there is a small demo on how to read and write data using the Health Kit Api. (you have to watch this video using the Safari browser)

The first step is quite simple. After you import the Health Kit library at the top of you class, just add this small piece of code in an InterfaceController inside your Watch Extension folder. The following function should go in the willActivate() life cycle method:

    guard HKHealthStore.isHealthDataAvailable() else { return }

    let typeToShare = Set([HKObjectType.workoutType()])
    let typeToRead = Set([
         HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeartRate)!,
         HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierActiveEnergyBurned)!
         ])
        
    self.healthStore.requestAuthorizationToShareTypes(typeToShare, readTypes: typeToRead) {
         success, error in
         // error handling
            print(error)
    } 

As you can see, we first check if the Health Data is available using Swift’s guard statement. If available we need to create Sets of data to read and write and call the request authorization method.

That call is going to be received by the AppDelegate Class in your companion app, so you need to write the following method there:

  func applicationShouldRequestHealthAuthorization(application: UIApplication) {
      self.healthStore.handleAuthorizationForExtensionWithCompletion {
          success, error in  
            // HANDLE ERROR
            print("there is something wrong with the authorization")
        }
    }

Easy, right? But this is where things can get a bit weird. If you try to run this code in your Xcode simulator you might see this in you console:

Missing com.apple.developer.healthkit entitlement.

If you search online for an answer you very quickly will find advice to check your project’s capabilities and turn on the Health’s kit option.

capability

However if you try to run the project now might still get the “Missing com.apple.developer.healthkit entitlement.” warning.

The problem is that a big percentage of the answers you are going to find are not WatchOS specific and they refer to access the Health Kit capabilities from your IOS apps. After a few hours completely clueless, a colleague of mine found the solution. It happens that when you add a WatchOs extension to your app, you also get new set WatchOs extension capabilities that can be edited.

capabilities_watch

If your run your code now, you should see a notification on your Apple Watch and an alert in your Iphone that will prompt you to a Health Kit view where you can authorize or not your request.

access

Hope this helps!

Share on Facebook2Tweet about this on TwitterShare on LinkedIn2

Leave a Comment

Your email address will not be published. Required fields are marked *