New! Check out Ionic Native. It's like ngCordova but for ES6 and TypeScript.

$cordovaHealthKit

Cordova Plugin For accessing and modifying HealthKit data in iOS apps (iOS only).

Note: make sure to enable HealthKit entitlement through X Code in the Capabilities settings for your app.

cordova plugin add https://github.com/Telerik-Verified-Plugins/HealthKit.git

Methods

isAvailable()

Check if HealthKit is available.

Example

module.controller('ThisCtrl', function($scope, $cordovaHealthKit) {
  $cordovaHealthKit.isAvailable().then(function(yes) {
    // Is available
  }, function(no) {
    // Is not available
  });
})
requestAuthorization(readTypes, writeTypes)

Request authorization to the specified data types for reading and writing in HealthKit.

Param Type
readTypes Array
writeTypes Array

readTypes and writeTypes are arrays of HealthKit Constants. See the iOS Docs for the full list.

Example

module.controller('ThisCtrl', function($scope, $cordovaHealthKit) {

  // Requests read privileges for DOB, active energy burned, and height, and write privs for
  // the same plus cycling

  $cordovaHealthKit.requestAuthorization(
    [
      'HKCharacteristicTypeIdentifierDateOfBirth',
      'HKQuantityTypeIdentifierActiveEnergyBurned',
      'HKQuantityTypeIdentifierHeight'
    ],
    [
      'HKQuantityTypeIdentifierActiveEnergyBurned',
      'HKQuantityTypeIdentifierHeight',
      'HKQuantityTypeIdentifierDistanceCycling'
    ]
  ).then(function(success) {
    $scope.granted = true;
  }, function(err) {
    $scope.granted = false;
  });
})
readDateOfBirth()

Read the Date of Birth field from HealthKit.

Example

$cordovaHealthKit.readDateOfBirth().then(function(dob) {
  // value
}, function(err) {
});
readGender()

Read the user’s Gender.

Example

$cordovaHealthKit.readGender().then(function(gender) {
  // value
}, function(err) {
});
readWeight(units)

Read the most recent Weight data point from HealthKit.

Param Type
units unit_string

See the HKUnit Class Reference for possible units. Defaults to 'lb'

Example

$cordovaHealthKit.readWeight('kg').then(function(v) {
  /*
  v is of the form:

  {
    'unit': 'kg',
    'value': '200',
    'date': '2014-11-22 09:33:00'
  }
  */
}, function(err) {
});
saveWeight(value, units, date)

Save a weight data point with the given value, units, and date of sample.

Param Type
value Number
units unit_string
date Date

See the HKUnit Class Reference for possible units. Defaults to 'lb'

Date is a JS Date, defaults to the current time.

Example

$cordovaHealthKit.saveWeight(180, 'kg').then(function(v) {
}, function(err) {
});
readHeight(units)

Read the most recent Height data point from HealthKit.

Param Type
units unit_string

See the HKUnit Class Reference for possible units. Defaults to 'in'

Example

$cordovaHealthKit.readHeight('cm').then(function(v) {
  /*
  v is of the form:

  {
    'unit': 'cm',
    'value': '200',
    'date': '2014-11-22 09:33:00'
  }
  */
}, function(err) {
});
saveHeight(value, units, date)

Save a height data point with the given value, units, and date of sample.

param type
value number
units unit_string
date Date

See the HKUnit Class Reference for possible units. Defaults to 'lb'

Date is a JS Date, defaults to the current time.

Example

$cordovaHealthKit.saveHeight(180, 'cm').then(function(v) {
}, function(err) {
});
findWorkouts()

Query for all workout data.

Example

$cordovaHealthKit.findWorkouts().then(function(v) {
}, function(err) {
});
saveWorkout(workout)

Save a workout.

param type
workout Object

Workout must be of the form:

{
  'activityType': 'HKWorkoutActivityTypeCycling', // HKWorkoutActivityType constant (https://developer.apple.com/library/ios/documentation/HealthKit/Reference/HKWorkout_Class/#//apple_ref/c/tdef/HKWorkoutActivityType)
  'quantityType': 'HKQuantityTypeIdentifierDistanceCycling',
  'startDate': new Date(), // mandatory
  'endDate': null, // optional, use either this or duration
  'duration': 3600, // in seconds, optional, use either this or endDate
  'energy': 300, //
  'energyUnit': 'kcal', // J|cal|kcal
  'distance': 11, // optional
  'distanceUnit': 'km' // probably useful with the former param
  // 'extraData': "", // Not sure how necessary this is
},

See the Activity Type and HK Constants reference for more info.

Example

$cordovaHealthKit.saveWorkout(
  {
    'activityType': 'HKWorkoutActivityTypeCycling', // HKWorkoutActivityType constant (https://developer.apple.com/library/ios/documentation/HealthKit/Reference/HKWorkout_Class/#//apple_ref/c/tdef/HKWorkoutActivityType)
    'quantityType': 'HKQuantityTypeIdentifierDistanceCycling',
    'startDate': new Date(), // mandatory
    'endDate': null, // optional, use either this or duration
    'duration': 3600, // in seconds, optional, use either this or endDate
    'energy': 300, //
    'energyUnit': 'kcal', // J|cal|kcal
    'distance': 11, // optional
    'distanceUnit': 'km' // probably useful with the former param
    // 'extraData': "", // Not sure how necessary this is
  }
).then(function(v) {
}, function(err) {
});