Ios HealthKit错误“;受保护的健康数据不可访问“;?

Ios HealthKit错误“;受保护的健康数据不可访问“;?,ios,swift,healthkit,Ios,Swift,Healthkit,在尝试从iPhone上的HealthKit加载训练数据时,我偶尔(并非每次)遇到错误:error Domain=com.apple.HealthKit code=6“无法访问受保护的健康数据”{NSLocalizedDescription=无法访问受保护的健康数据} 我发现的唯一其他关于这个错误的引用是 但我正在尝试在手机解锁的情况下加载数据。还有什么问题吗 我的HealthKit权限: private func requestAccessToHealthKit() { let

在尝试从iPhone上的HealthKit加载训练数据时,我偶尔(并非每次)遇到错误:
error Domain=com.apple.HealthKit code=6“无法访问受保护的健康数据”{NSLocalizedDescription=无法访问受保护的健康数据}

我发现的唯一其他关于这个错误的引用是 但我正在尝试在手机解锁的情况下加载数据。还有什么问题吗

我的HealthKit权限:

private func requestAccessToHealthKit() {
        let healthStore = HKHealthStore()

        let healthKitTypesToWrite: Set<HKSampleType> = [HKObjectType.workoutType(),
            HKSeriesType.workoutRoute(),
            HKObjectType.quantityType(forIdentifier: .activeEnergyBurned)!,
            HKObjectType.quantityType(forIdentifier: .heartRate)!,
            HKObjectType.quantityType(forIdentifier: .restingHeartRate)!,
            HKObjectType.quantityType(forIdentifier: .bodyMass)!,
            HKObjectType.quantityType(forIdentifier: .vo2Max)!,
            HKObjectType.quantityType(forIdentifier: .stepCount)!,
            HKObjectType.quantityType(forIdentifier: .distanceWalkingRunning)!]

        let healthKitTypesToRead: Set<HKObjectType> = [
            HKObjectType.workoutType(),
            HKSeriesType.workoutRoute(),
            HKObjectType.quantityType(forIdentifier: .activeEnergyBurned)!,
            HKObjectType.quantityType(forIdentifier: .heartRate)!,
            HKObjectType.quantityType(forIdentifier: .restingHeartRate)!,
            HKObjectType.characteristicType(forIdentifier: .dateOfBirth)!,
            HKObjectType.quantityType(forIdentifier: .bodyMass)!,
            HKObjectType.quantityType(forIdentifier: .vo2Max)!,
            HKObjectType.quantityType(forIdentifier: .stepCount)!,
            HKObjectType.quantityType(forIdentifier: .distanceWalkingRunning)!]


        healthStore.requestAuthorization(toShare: healthKitTypesToWrite, read: healthKitTypesToRead) { (success, error) in
            if !success {
                print("failed HealthKit Authorization from iPhone \(String(describing: error?.localizedDescription))")
            }

            print("Successful HealthKit Authorization from iPhone")
        }
    }
private func requestAccessToHealthKit(){
让healthStore=HKHealthStore()
让HealthKittypeTowRite:Set=[HKObjectType.workoutType(),
HKSeriesType.workoutRoute(),
HKObjectType.quantityType(用于标识符:.activeEnergyBurned)!,
HKObjectType.quantityType(用于标识符:。心率)!,
HKObjectType.quantityType(用于标识符:.restingheartate)!,
HKObjectType.quantityType(用于标识符:。体质量)!,
HKObjectType.quantityType(用于标识符:.vo2Max)!,
HKObjectType.quantityType(用于标识符:.stepCount)!,
HKObjectType.quantityType(用于标识符:.distanceWalkingRunning)!]
让healthKitTypesToRead:Set=[
HKObjectType.workoutType(),
HKSeriesType.workoutRoute(),
HKObjectType.quantityType(用于标识符:.activeEnergyBurned)!,
HKObjectType.quantityType(用于标识符:。心率)!,
HKObjectType.quantityType(用于标识符:.restingheartate)!,
HKObjectType.characteristicType(用于标识符:。出生日期)!,
HKObjectType.quantityType(用于标识符:。体质量)!,
HKObjectType.quantityType(用于标识符:.vo2Max)!,
HKObjectType.quantityType(用于标识符:.stepCount)!,
HKObjectType.quantityType(用于标识符:.distanceWalkingRunning)!]
requestAuthorization(toShare:healthKitTypesToWrite,read:healthKitTypesToRead){(成功,错误)在
如果!成功{
打印(“来自iPhone的HealthKit授权失败\(字符串(描述:错误?.localizedDescription)))
}
打印(“从iPhone成功获得HealthKit授权”)
}
}

我偶尔也会遇到类似的问题。你在这上面找到什么了吗?@AdeeshJain我没有……它只是偶尔出现。如果你有什么发现,请告诉我。我有一个这样的零星错误。奇怪的是,一旦收到HealthKit数据,watchOS就会将其完全锁定。即使授权状态返回true,也无法访问数据。