如果用户不允许某些条目类型,HealthKit 无法使用多个条目编写 HKSample
HealthKit unable to write HKSample with multiple entries if some of the entry types are disallowed by user
假设一个应用请求授权将脂肪和碳水化合物写入 HealthKit:
func dataTypesToWrite() -> NSSet {
return NSSet(objects:
HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.dietaryCarbohydrates)!,
HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.dietaryFatTotal)!
)
}
healthStore.requestAuthorization(toShare: dataTypesToWrite() as? Set<HKSampleType>, read: nil, completion: { (success, error) -> Void in
if success {
print("completed")
}
})
这将提示用户允许应用写入 HealthKit。如果用户允许同时写入脂肪和碳水化合物,则一切都很好。但是,如果他们只选择允许一个,并且将带有脂肪和碳水化合物的 HKSample 写入 HealthKit,则该条目将不会显示:
func foodCorrelationForFoodItem(foodNutrients: Dictionary<String, Double>, foodTitle: String) -> HKCorrelation {
let nowDate: Date = Date()
let consumedSamples: Set<HKSample> = [
HKQuantitySample(
type: HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.dietaryCarbohydrates)!,
quantity: HKQuantity(unit: HKUnit.gram(), doubleValue: 5.0),
start: nowDate,
end: nowDate),
HKQuantitySample(
type: HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.dietaryFatTotal)!,
quantity: HKQuantity(unit: HKUnit.gram(), doubleValue: 10.0),
start: nowDate,
end: nowDate)
]
let foodType: HKCorrelationType = HKCorrelationType.correlationType(forIdentifier: HKCorrelationTypeIdentifier.food)!
let foodCorrelationMetadata: [String: AnyObject] = [HKMetadataKeyFoodType: foodTitle as AnyObject]
let foodCorrelation: HKCorrelation = HKCorrelation(type: foodType, start: nowDate, end: nowDate, objects: consumedSamples, metadata: foodCorrelationMetadata)
return foodCorrelation
}
self.healthStore.save(foodCorrelationForFoodItem) { (success, error) in
if let error = error {
print(error)
}
}
由于 Apple 不允许应用程序查看哪些 HealthKit 项目是可写的,因此无法检测到例如应该只写脂肪。有针对这个的解决方法吗?谢谢
Apple 不允许应用查询它可以写入哪些 HealthKit 类型,这不是真的。您可能会想到不可查询的读取授权。您可以在 HKHealthStore
到 determine whether your app has write authorization 上使用 authorizationStatus(for:)
。如果它 returns notDetermined
或 sharingDenied
那么您的应用没有授权。
假设一个应用请求授权将脂肪和碳水化合物写入 HealthKit:
func dataTypesToWrite() -> NSSet {
return NSSet(objects:
HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.dietaryCarbohydrates)!,
HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.dietaryFatTotal)!
)
}
healthStore.requestAuthorization(toShare: dataTypesToWrite() as? Set<HKSampleType>, read: nil, completion: { (success, error) -> Void in
if success {
print("completed")
}
})
这将提示用户允许应用写入 HealthKit。如果用户允许同时写入脂肪和碳水化合物,则一切都很好。但是,如果他们只选择允许一个,并且将带有脂肪和碳水化合物的 HKSample 写入 HealthKit,则该条目将不会显示:
func foodCorrelationForFoodItem(foodNutrients: Dictionary<String, Double>, foodTitle: String) -> HKCorrelation {
let nowDate: Date = Date()
let consumedSamples: Set<HKSample> = [
HKQuantitySample(
type: HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.dietaryCarbohydrates)!,
quantity: HKQuantity(unit: HKUnit.gram(), doubleValue: 5.0),
start: nowDate,
end: nowDate),
HKQuantitySample(
type: HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.dietaryFatTotal)!,
quantity: HKQuantity(unit: HKUnit.gram(), doubleValue: 10.0),
start: nowDate,
end: nowDate)
]
let foodType: HKCorrelationType = HKCorrelationType.correlationType(forIdentifier: HKCorrelationTypeIdentifier.food)!
let foodCorrelationMetadata: [String: AnyObject] = [HKMetadataKeyFoodType: foodTitle as AnyObject]
let foodCorrelation: HKCorrelation = HKCorrelation(type: foodType, start: nowDate, end: nowDate, objects: consumedSamples, metadata: foodCorrelationMetadata)
return foodCorrelation
}
self.healthStore.save(foodCorrelationForFoodItem) { (success, error) in
if let error = error {
print(error)
}
}
由于 Apple 不允许应用程序查看哪些 HealthKit 项目是可写的,因此无法检测到例如应该只写脂肪。有针对这个的解决方法吗?谢谢
Apple 不允许应用查询它可以写入哪些 HealthKit 类型,这不是真的。您可能会想到不可查询的读取授权。您可以在 HKHealthStore
到 determine whether your app has write authorization 上使用 authorizationStatus(for:)
。如果它 returns notDetermined
或 sharingDenied
那么您的应用没有授权。