卫生工具包许可权返回错误的值ios错误?

时间:2019-04-02 13:27:55

标签: ios swift health-kit

嘿,让我们直达重点

问题是用户拒绝健康包的许可权时,先使用requestAuthorization询问,然后稍后从设置中授予许可权,但是authorizationStatus({ {3}}返回 sharingdenied (即使共享已授予许可)

复制步骤

添加以下内容以请求许可,然后按“不允许”

 class func authorizeHealthKit(completion: @escaping (Bool, Error?) -> Swift.Void) {

    guard HKHealthStore.isHealthDataAvailable() else {
        completion(false,HealthkitSetupError.notAvailableOnDevice)
        return
    }

    guard   let dateOfBirth = HKObjectType.characteristicType(forIdentifier: .dateOfBirth),
            let bloodType = HKObjectType.characteristicType(forIdentifier: .bloodType),
            let gender = HKObjectType.characteristicType(forIdentifier: .biologicalSex),

            let bodyMassIndex = HKObjectType.quantityType(forIdentifier: .bodyMassIndex),
            let height = HKObjectType.quantityType(forIdentifier: .height),
            let bodyMass = HKObjectType.quantityType(forIdentifier: .bodyMass),
            let activeEnergy = HKObjectType.quantityType(forIdentifier: .activeEnergyBurned)


    else {
        completion(false,HealthkitSetupError.dataTypeNotAvailable)
        return
    }





    let healthKitTypesToWrite:Set<HKSampleType> = [bodyMassIndex,
                                                    activeEnergy,
                                                    HKObjectType.workoutType()
                                                    ]

    let healthKitTypesToRead:Set<HKObjectType> = [dateOfBirth,
                                                  bloodType,
                                                  gender,
                                                  height,
                                                  bodyMass,
                                                  HKSampleType.workoutType()

                                                    ]


    HKHealthStore().requestAuthorization(toShare: healthKitTypesToWrite, read: healthKitTypesToRead) { (granted, error) in
        completion(granted,error)
    }

现在打开Health Kit应用程序(在“源”选项卡中)并允许所有权限

返回应用程序和

添加以下代码

 let array = [HKObjectType.characteristicType(forIdentifier: .dateOfBirth),
                 HKObjectType.characteristicType(forIdentifier: .bloodType),
                 HKObjectType.characteristicType(forIdentifier: .biologicalSex),

                 HKObjectType.quantityType(forIdentifier: .bodyMassIndex),
                 HKObjectType.quantityType(forIdentifier: .height),
                 HKObjectType.quantityType(forIdentifier: .bodyMass),
                 HKObjectType.quantityType(forIdentifier: .activeEnergyBurned)]


    for item in array {

        let type =  store.authorizationStatus(for: item!)

        switch type  {
        case .sharingDenied:
            print("Sharing Denied \(item)")
        case .notDetermined:
            print("Not Determined \(item)")
        case .sharingAuthorized:
            print("Authorized \(item)")

        }

    }

您将看到其中一些值是 Authorized (已授权),但大多数都是 Sharing Denied (共享被拒绝)。但是,您可以从Health Kit中读取数据

请建议

0 个答案:

没有答案