删除Health App中添加的样本

Bog*_*nov 8 sample ios swift healthkit

我添加这样的样本:

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!")
            }
        })
Run Code Online (Sandbox Code Playgroud)

当我想删除这个示例时,我执行:

store.deleteObject(sample, withCompletion: {(success, error) -> Void in
    if(error != nil) {
        println("Error deleting sample: \(error.localizedDescription)")
    }else{
        println("Sample deleted successfully!")
    }
})
Run Code Online (Sandbox Code Playgroud)

并且它返回我:删除样本时出错:找不到对象每次我对startDate和endDate使用相同的'date'.我尝试过let sample = HKQuantitySample(type: type, quantity: quantity, startDate: date, endDate: date, metadata: metadata)元数据的位置let metadata = [HKMetadataKeyExternalUUID:"\(Int64(date.timeIntervalSince1970))"],但也没有成功......

Bha*_*ash 2

除 Apple 的健康应用程序外,无法删除应用程序 A 在应用程序 B 中输入的健康数据。

根据 Apple 的文档,可以删除仅由相应应用程序创建的健康数据。虽然用户获得写入权限,但无法从您的应用程序中删除某些其他应用程序的健康数据,但只能从 Apple 的健康应用程序中删除。

文档中:

笔记

尽管您的应用程序只能管理它创建和保存的对象,但用户可以使用“健康”应用程序删除他们想要的任何数据。