HealthKit未经授权与集合中的0条记录

时间:2016-02-21 22:42:13

标签: swift ios9 health-kit hkhealthstore

IOS 9.2.1,Swift 2.1

我尝试在访问HealthKit时向用户提供合理的错误消息,并为查询返回0条记录。

可能是在所选时间范围内没有记录,或者可能是用户不允许访问健康状况内的特定数据集。在这两种情况下," storage.requestAuthorizationToShareTypes"提供了一个成功的"价值为真。

有没有办法让HKHealthKit商店给我一个代码,表明访问已被禁用?

我的代码

谢谢Mike

import Foundation
import HealthKit

// Interface to the HealthKit

class HealthKitIF {

let storage = HKHealthStore()
var stepsEnabled = false
var bgEnabled = false
var hkSupported =  false

init () {
        self.checkAuthorization()
}

func checkAuthorization () -> Bool {
    // Default to assuming that we're authorized
    var isEnabled = true

    if (NSClassFromString("HKHealthStore") != nil) { hkSupported = true }

    // Do we have access to HealthKit on this device?
    if ((hkSupported) && (HKHealthStore.isHealthDataAvailable())) {
       // We have to request each data type explicitly

      // Ask for BG
       var readingsSet = Set<HKObjectType>()
        readingsSet.insert(HKQuantityType.quantityTypeForIdentifier(HKQuantityTypeIdentifierBloodGlucose)!)
        readingsSet.insert(HKQuantityType.quantityTypeForIdentifier(HKQuantityTypeIdentifierStepCount)!)
        storage.requestAuthorizationToShareTypes(nil, readTypes: readingsSet) { (success, error) -> Void in
            isEnabled = success
            self.bgEnabled = success
        }

     }
    else
    {
        isEnabled = false
    }

    return isEnabled
}

1 个答案:

答案 0 :(得分:1)

如果查询没有结果,您的应用不应出现错误消息。 HealthKit旨在通过不区分未授权访问和无数据来保持用户的读取授权选择的私密性。但是,在应用程序或支持页面的某个位置添加提醒可能会有所帮助,这些提醒描述了如果用户未在您的应用中看到预期行为,用户可以如何调整其健康隐私设置。

来自HKHealthStore class reference

  

为了帮助防止敏感健康信息泄露,您的   应用无法确定用户是否已授予阅读权限   数据。如果您未获得许可,则只会出现在那里   没有HealthKit商店中请求的类型的数据。如果你的应用程序   获得共享权限但未获得读取权限,您只能看到   您的应用已写入商店的数据。来自其他来源的数据   仍然隐藏着。