Swift 4 从HealthKit获取首选单位



我是 Swift 的初学者,我试图从 HealthKit 获得用户首选单元。

苹果为其提供了此功能

func preferredUnits(for quantityTypes: Set<HKQuantityType>, 
completion: @escaping ([HKQuantityType : HKUnit], Error?) -> Void)

但我真的不知道如何正确实现它。ATM功能在我的应用程序中不做任何事情。

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let bloodGlucose = HKObjectType.quantityType(forIdentifier: .bloodGlucose)
let HKQuantitySet: Set<HKSampleType> = [bloodGlucose!]        
self.preferredUnits(for: HKQuantitySet as! Set<HKQuantityType>, completion: { (preferedUnit, error) in
print(preferedUnit)
})
return true
}

func preferredUnits(for quantityTypes: Set<HKQuantityType>,
completion: @escaping ([HKQuantityType : HKUnit], Error?) -> Void){
}

我很确定我完全错了。

有人知道如何正确实现该功能吗?不仅对于bloodglucose,总的来说,会很好。

您需要将preferredUnits函数称为HKHealthStore的实例。 您的代码只是调用您使用相同签名声明的函数,该函数不执行任何操作。

最新更新