HealthKit Permission Sheet Not Appearing

亡梦爱人 提交于 2019-12-10 02:24:54

问题


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



来源:https://stackoverflow.com/questions/40777457/healthkit-permission-sheet-not-appearing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!