There are many examples on how to add heart rate or step counts to Apple Health via HealthKit. I made an app that lets you track your sexual activity. And now I'd like to add HealthKit support.
After creating the app in Xcode, add the HealthKit capability and add the string to your Info.plist.
For clarity and better practice we'll create a class that'll handle the connection to HealthKit. You could add this to a view too. It would be ugly.
A sample is what is written to
HKHealthStore and an object is what is read from
HealthConnector class facilitates interaction with
HKHealthStore. It currently has two three functions:
Before any interaction with
HKHealthStore we must ask for permission. It's not enough to ask once. The user could revoke permission when we're not looking.
You have to specifically ask for the Sample Types you'd like to read from and write to. Since my app only writes samples but won't retrieve anything,
You'll have to delete the old object and save a new sample.