Links

Health Kit APIs at a glance

Health Kit APIs are designed to work with existing Health Apps, with this API app can share statistical analysis (e.g; graphs, trends..), user information etc.. and user can check it from iOS "Health" app
With Health Kit API we can create, save and ask for users health and fitness related data which is centrally managed by iOS "Health" app. This can be used for surfacing data from other health app and read/write your own data. There are some basics we need to know to get started with the same;
  1. HKUnit - It represents a particular unit that can be accessed in any conversion (eg; Kg, Lb, Gram,..)
  2. HKQuantity - A double value related to a HKUnit. Note: there will be an excecption if you convert different units (e.g; Kg -> Liter), you can ask for compatibility (isCompatiableWithUnit:) if you are not sure
  3. HKObjectType - It represents different kind of data that we can store in HealthKit (e.g; Steps, Calories, BMI, Blood Pressure, etc..), it is a superclass for all Health Kit data types, i.e;
    • HKCharactersticType - fixed user info like blood type, date of birth, gender
    • HKSampleType - variable user info, it is consist of;
      • HKQuantityType - (for Steps, Calories, etc.)
      • HKCategoryType - used to categorize HKQuantityType, its paired with corresponding enum
    HKObjectType is immutable and its properties are read-only.
We need HKHealthStore to save data, it is kinda link to the database. It will let you save objects and query for data. Usage of multiple HKHealthStore is not recommended. Its easy to ask for HKCharactersticType info of user because HKHealthStore have direct methods for the same, but for complex info there are queries, i.e;
  1. HKQuery - we can use predicates to search for our needs.
  2. HKObserverQuery - watches for changes in the database, it will called each and every something changed in database related to your query. It supports background delivery.
  3. HKAnchoredObjectQuery - it will help in paging of data, it is consist of Anchor and Limit. Anchor is a simple starting point. If we set Anchor to zero if will return all data with limit, for no limit we can use constant "HKObjectQueryNoLimit". Complition handler will have a new Anchor which we can use in our next query
Once we create a query we can execute it with the help of HKHealthStore, it have method two methods "-(void)executeQuery:(HKQuery *)query;" and "-(void)stopQuery:(HKQuery *)query;". We can call a query only once because its callbacks are invalidated once query is stopped. We only need to stop HKObserverQuery because other query will execute callback method only one time.

We can use HKStatics to get required quantity data from the query output, we can ask statictics for all kinda data or a particular HKSource data. There are two kinda quantity data, i.e;
  1. Discrete - Min, Max, Average (e.g; user's average weight in a week)
  2. Cumulative - Sum (e.g; sum of steps taken in a perticular interval)
We can filter statictics with the help of HKStatisticsQuery, it takes options as HKStatisticsOptions and returns HKStatistics object. List of HKStatistics objects can be kept in HKStatisticsCollection, which can be further used based on time intervals. HKStatisticsCollectionQuery is used to fetch usefull info from HKStatisticsCollection, it is consist of Statictics options, Anchor data and interval.

Good to know

  1. We need to activate HealthKit in your project settings
  2. We need to ask for authorization to read/write health data saparately for each and every type of info
  3. Always check of access authorization because user can manage anytime from outside the app
  4. An app can't get status for read access to the user info because of Apple's Privacy Policy
  5. We need to localize the units ourself, we can do so with the help of NSMassFormatter, NSLengthFormatter and NSEnergyFormatter

Sample App

Fit Store and Retrieve Health Kit Data - Sample App is avilabe at Apple developer portal you can download it from here.

Post a Comment