HealthKit Permission Sheet Not Appearing

2019-04-22 13:21发布

问题:

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.

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")

回答1:

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. .