Fetching Health Data from HealthKit in iOS .

Posted in: Blog

One of Appdev360’s clients required HealthKit SDK to be integrated into his iOS application. HealthKit is an iOS framework that provides an easy-to-read dashboard for health information. It allows healthcare and fitness apps to work together and share their data in the Health app. Appdev360 engineer, Inaam Ur Rehman decided to get access to the health data from HealthKit and integrate it within the client’s application.
Our Implementation

Firstly, Inaam Ur Rehman navigated to Target > Capabilities and enabled the HealthKit to set up the project as shown below:

HealthKit-1

After setting up the project, Inaam took the following steps to gain authorization of the HealthKit data from the user:

  • Created a set for the type of data user want to read i.e. HKQuantityTypeIdentifierHeight etc.
  • Created a set for the type of data user want to read i.e. HKQuantityTypeIdentifierDietarySugar etc.
  • Checked if the HealthKit data is available in the device using HKHealthStore.isHealthDataAvailable() method
  • Finally, used the HealthKitStore’s method requestAuthorizationToShareTypes() to gain authorization

.

The following screen appeared after executing the steps mentioned above:

HealthKit-2

Using the default HealthKit app in the iOS 8.0 version, Inaam created some sample data points after gaining authorization. The following screenshot displays sample data points:

HealthKit-3

The final step was to fetch the health data within the application. Inaam utilized HealthKit SDK methods to gain access to the required parameters and took the following steps to fetch the  values:

  • Created an NSPredicate
  • Created an HKSampleQuery object and passed it along the other required parameters
  • Executed the query using the HealthKitStore’s executeQuery method

HealthKit-4
Note: The ExecuteQuery method works ‘asynchronously’ and is to be handled appropriately. Once executed successfully, you will be able to retrieve values from the HealthKit. Only those values will be exposed to the application to which a user has granted access.

Leave a Reply

Your email address will not be published. Required fields are marked *