ios - 使用 HealthKit 检索 SleepAnalysis 后,我仍然收到此授权未确定错误

标签 ios swift healthkit hksamplequery

几天来我一直在使用对此错误的引用,现在试图找出解决方案:

[query] 激活查询时出错:Error Domain=com.apple.healthkit Code=5“未确定授权”UserInfo={NSLocalizedDescription=未确定授权}

我一直在使用 HealthKit 成功检索 sleep 数据,但现在我需要检索事件数据。我用这个函数设置了 HealthKit:

let typesToRead = Set([
        HKObjectType.categoryType(forIdentifier: HKCategoryTypeIdentifier.sleepAnalysis)!,
        HKObjectType.categoryType(forIdentifier: HKCategoryTypeIdentifier.appleStandHour)!,
        HKObjectType.characteristicType(forIdentifier: HKCharacteristicTypeIdentifier.dateOfBirth)!,
        HKObjectType.characteristicType(forIdentifier: HKCharacteristicTypeIdentifier.bloodType)!,
        HKObjectType.characteristicType(forIdentifier: HKCharacteristicTypeIdentifier.biologicalSex)!,
        HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.height)!,
        HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.bodyMass)!,
        HKObjectType.workoutType(),
        HKObjectType.activitySummaryType()
        ])

self.healthStore.requestAuthorization(toShare: nil, read: typesToRead) { (sucess, error) -> Void in
        if sucess == false {
            NSLog("Error...")
        }
    }

然后我创建查询,例如:

let query = HKSampleQuery(sampleType: distanceType, predicate: nil, limit: 0, sortDescriptors: [startDateSort]) {
        (sampleQuery, results, error) -> Void in

        if let result = results {
            for item in result {
                if let sample = item as? HKQuantitySample {
                    self.workOutSamples.append(sample)
                }
            }
            print(self.workOutSamples)
        }
    }
    healthStore.execute(query)

我已将隐私 - 健康更新使用说明隐私 - 健康共享使用说明添加到 info.plist 并且我的应用程序的功能包括已成功启用 HealthKit。

最佳答案

向 typesToRead 添加了 HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.distanceWalkingRunning)!。我的帖子没有显示我在创建 distanceType 时使用了错误的类型:quantityType

关于ios - 使用 HealthKit 检索 SleepAnalysis 后,我仍然收到此授权未确定错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44417472/

相关文章:

ios - HealthKit:为特定来源和日期范围创建谓词

ios - 设置 HKAnchoredObjectQuery 以便我只接收自上次查询以来的更新?

ios - @media 查询以排除 SASS 中的 iOS 设备

ios - 无法使用 Swift 连接到远程 RabbitMQ 服务器

ios - 如何在 Swift 中按名称/标识符从相机胶卷中获取图像

ios - 如何从字符串数组中删除图像?

ios - 在 CollectionView 上滑动删除

ios - 如何为 UITextView Swift 添加下划线/粗体/斜体功能

ios - 如何正确跟踪 HealthKit 传统力量训练

iphone - NSDocumentDirectory如何工作?