Delete added sample in Health App Delete added sample in Health App swift swift

Delete added sample in Health App


It is not possible to delete the Health data which is entered by ApplicationA in ApplicationB, other than Apple's Health app.

As per Apple's documentation, it is possible to delete the health data which is created by the respective application only. Though, user given write permission, it is not possible to delete some other applications health data from your application, but it is possible from Apple's Health application only.

From 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.


I actually got some help from Apple with this exact problem, but I haven't yet solved it. You are coding this the exact same way I did, but they told me the problem is that that we are creating a new sample and then trying to delete it immediately. The sample doesn't exist yet, so it returns an item not found error.

What needs to be done (in theory) is to run an HKSampleQuery and then perform the delete with the object returned from that. That object already exists in HealthKit and can be deleted.

Hope that helps!

EDIT : Solved it... I found that my date/time stamp was TOO strict. I modified my startDate / endDate to return MM/DD/YYYY format and samples were deleted properly. Hope that helps you!