12

In my watch extension I call this function:

func requestAuthorization() {
        let healthStore = HKHealthStore()
        let workoutType = HKObjectType.workoutType()
        let heartRateType = HKObjectType.quantityType(forIdentifier: .heartRate)

        //reading
        let readingTypes = Set([heartRateType!, workoutType])

        //writing
        let writingTypes = Set([heartRateType!, workoutType])

        //auth request
        healthStore.requestAuthorization(toShare: writingTypes, read: readingTypes) { (success, error) -> Void in

            if error != nil {
                print("error \(error?.localizedDescription)")
            } else if success {
                self.startButton.setEnabled(true)
            } else if !success {
                self.startButton.setEnabled(false)
            }
        }
    }

And in AppDelegate.swift, I have:

func applicationShouldRequestHealthAuthorization(_ application: UIApplication) {
        let healthStore = HKHealthStore()
        healthStore.handleAuthorizationForExtension { (success, error) -> Void in
            //...
        }
    }

I get the dialog on my watch and phone and it opens the app on the phone when I tell it to from the dialog. The issue I'm having is that the phone app doesn't display the permission sheet that should show up in order to allow permissions. The permission sheet is mentioned here: https://developer.apple.com/reference/healthkit

What do I need to do to get it to appear so permissions can be granted? As it stand right now, I can grant permissions by going to the Health app then sources and select my app and grant the permissions that way.

The permission sheet I'm talking about is this. enter image description here

EDIT: I'm getting this error printed from the requestAuthorization() method call.

error Optional("Required usage description strings not present in app\'s Info.plist")
raginggoat
  • 3,570
  • 10
  • 48
  • 108

2 Answers2

0

With this TestHealthKit, I was able to open the health-kit permission sheet.

        // 4.  Request HealthKit authorization
    (HealthStore as! HKHealthStore).requestAuthorizationToShareTypes(nil, readTypes: dataTypesToRead()) { (success, error) -> Void in

        //            self.CheckAuthorizationStatusFor(self.dataTypesToRead())
        if (success)
        {

            SuccessCompletion(success: success)
            return;
        }
        else
        {

            FailureCompletion(err: error)
            return;
        }

    }

Also, check have you enabled the entitlement as shown. entitlements .

itechnician
  • 1,645
  • 1
  • 14
  • 24
  • That's exactly what I have and the entitlement is on. Everything in my app works if I go to the Apple Health app and grant permissions to my app from there. Literally the only issue I'm having is not seeing the permission sheet when the watch app requests permission from the phone and when I select to open the app from the alert on the phone, it opens my app but I don't see the permission sheet. – raginggoat Nov 24 '16 at 15:18
-1

As stated in the documentation, you must add the necessary keys to .plist if your app uses APIs that read or update user health data.

NSHealthShareUsageDescription - A message to the user that explains why the app requested permission to read samples from the HealthKit store.

NSHealthUpdateUsageDescription - A message to the user that explains why the app requested permission to save samples to the HealthKit store.