忽略来自 Apple Health 应用程序的手动条目作为数据源

Ignore manual entries from Apple Health app as Data Source

您好,我正在编写一个健身应用程序,它从 Apples Health 应用程序获取数据。

到目前为止一切顺利。

问题:在健康应用程序中,可以手动输入数据,这使得作弊成为可能。

问题:如何排除或忽略此特定数据条目。

Just the Data with "Source: Health" so i've still the possibility to read data from a random Fitness tracker.

HealthKit 中由用户手动输入的示例将具有 HKMetadataKeyWasUserEntered 元数据键的 YES 值。要创建仅匹配 不是 用户输入的样本的谓词,您可以使用以下内容:

[NSPredicate predicateWithFormat:@"metadata.%K != YES", HKMetadataKeyWasUserEntered];

请注意,这必须表述为 value != YES,因为键的值可以是 YES、NO 或 nil,nil 表示 NO。

Swift 4:

let predicate = NSPredicate(format: "metadata.%K != YES", HKMetadataKeyWasUserEntered)

如果您有两个谓词,请使用 CompoundPredicate:

let compoundPredicate = NSCompoundPredicate(type: .and, subpredicates: [predicate1, predicate2])