1

IOS 9.2.1,斯威夫特 2.1

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

可能是在选定的时间范围内没有记录,或者可能是用户不允许访问健康中的特定数据集。在这两种情况下,“storage.requestAuthorizationToShareTypes”都提供了“成功”值 true。

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

我的代码如下

谢谢迈克

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
}
4

1 回答 1

1

当没有查询结果时,您的应用程序不应显示错误消息。HealthKit 旨在通过不区分未经授权的访问和无数据来使用户的读取授权选择保密。但是,在您的应用程序或支持页面中的某处包含提醒可能会有所帮助,该提醒说明用户在您的应用程序中没有看到预期行为时如何调整其健康隐私设置。

来自HKHealthStore 类参考

为帮助防止敏感健康信息可能泄露,您的应用无法确定用户是否已授予读取数据的权限。如果您没有被授予权限,它只是看起来好像在 HealthKit 存储中没有请求类型的数据。如果您的应用被授予共享权限但没有读取权限,则您只能看到您的应用写入商店的数据。来自其他来源的数据仍然隐藏。

于 2016-02-22T18:19:15.577 回答