我这样添加示例:
var store:HKHealthStore?
date = NSDate()
let type = HKQuantityType.quantityTypeForIdentifier(HKQuantityTypeIdentifierDietaryVitaminA)
quantity = HKQuantity(unit: HKUnit.gramUnitWithMetricPrefix(.Micro), doubleValue: 100)
let sample = HKQuantitySample(type: type, quantity: quantity, startDate: date, endDate: date)
store.saveObject(sample, withCompletion: { (success, error) -> Void in
if(error != nil) {
println("Error saving sample: \(error.localizedDescription)")
}else{
println("Sample saved successfully!")
}
})
当我想删除这个样本时,我执行:
store.deleteObject(sample, withCompletion: {(success, error) -> Void in
if(error != nil) {
println("Error deleting sample: \(error.localizedDescription)")
}else{
println("Sample deleted successfully!")
}
})
它返回给我:删除样本时出错:找不到对象
每次我对开始日期和结束日期使用相同的“日期”。
我试过 let sample = HKQuantitySample(type: type, quantity: quantity, startDate: date, endDate: date, metadata: metadata)
其中元数据是 let metadata = [HKMetadataKeyExternalUUID:"\(Int64(date.timeIntervalSince1970))"]
,但也不成功...
最佳答案
除了Apple的Health应用程序,无法删除ApplicationA在ApplicationB中输入的Health数据。
根据 Apple 的文档,可以删除仅由相应应用程序创建的健康数据。尽管用户获得了写入权限,但无法从您的应用程序中删除某些其他应用程序的健康数据,但只能从 Apple 的健康应用程序中删除。
来自 the documentation :
NOTE
Although your app can manage only the objects it created and saved, the users can delete any data they want using the Health app.
关于ios - 删除健康应用中添加的样本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28239685/