Commit b0d36545 authored by 冷佳娟's avatar 冷佳娟 💪

Merge branch 'terrillo' into develop

# Conflicts:
#	RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.h
#	RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.m
#	RCTAppleHealthKit/RCTAppleHealthKit+Queries.m
#	RCTAppleHealthKit/RCTAppleHealthKit+Utils.m
#	RCTAppleHealthKit/RCTAppleHealthKit.m
parents fbeb3980 5395cd79
......@@ -16,8 +16,46 @@ export const Permissions = {
BodyMassIndex: "BodyMassIndex",
BodyTemperature: "BodyTemperature",
DateOfBirth: "DateOfBirth",
DietaryEnergy: "DietaryEnergy",
Biotin: "Biotin",
Caffeine: "Caffeine",
Calcium: "Calcium",
Carbohydrates: "Carbohydrates",
Chloride: "Chloride",
Cholesterol: "Cholesterol",
Copper: "Copper",
EnergyConsumed: "EnergyConsumed",
FatMonounsaturated: "FatMonounsaturated",
FatPolyunsaturated: "FatPolyunsaturated",
FatSaturated: "FatSaturated",
FatTotal: "FatTotal",
Fiber: "Fiber",
Folate: "Folate",
Iodine: "Iodine",
Iron: "Iron",
Magnesium: "Magnesium",
Manganese: "Manganese",
Molybdenum: "Molybdenum",
Niacin: "Niacin",
PantothenicAcid: "PantothenicAcid",
Phosphorus: "Phosphorus",
Potassium: "Potassium",
Protein: "Protein",
Riboflavin: "Riboflavin",
Selenium: "Selenium",
Sodium: "Sodium",
Sugar: "Sugar",
Thiamin: "Thiamin",
VitaminA: "VitaminA",
VitaminB12: "VitaminB12",
VitaminB6: "VitaminB6",
VitaminC: "VitaminC",
VitaminD: "VitaminD",
VitaminE: "VitaminE",
VitaminK: "VitaminK",
Zinc: "Zinc",
Water: "Water",
DistanceCycling: "DistanceCycling",
DistanceSwimming: "DistanceSwimming",
DistanceWalkingRunning: "DistanceWalkingRunning",
FlightsClimbed: "FlightsClimbed",
HeartRate: "HeartRate",
......@@ -29,5 +67,6 @@ export const Permissions = {
SleepAnalysis: "SleepAnalysis",
StepCount: "StepCount",
Steps: "Steps",
Weight: "Weight"
Weight: "Weight",
Workout: "Workout"
}
Pod::Spec.new do |s|
s.name = "RCTAppleHealthKit"
s.summary = "A React Native bridge module for interacting with Apple Healthkit data"
s.version = "0.6.5"
s.homepage = "https://github.com/terrillo/rn-apple-healthkit"
s.license = "MIT"
s.author = { "Terrillo Walls" => "terrillo@terrillo.com" }
s.platform = :ios, "9.0"
s.source = { :git => "https://github.com/terrillo/rn-apple-healthkit", :tag => "master" }
s.source_files = "RCTAppleHealthKit/*.{h,m}"
s.requires_arc = true
s.dependency "React"
end
\ No newline at end of file
......@@ -16,6 +16,7 @@
3774C8D71D20C65F0000B3F3 /* RCTAppleHealthKit+Methods_Fitness.m in Sources */ = {isa = PBXBuildFile; fileRef = 3774C8D61D20C65F0000B3F3 /* RCTAppleHealthKit+Methods_Fitness.m */; };
377D44F31D247D0A004E35CB /* RCTAppleHealthKit+Methods_Characteristic.m in Sources */ = {isa = PBXBuildFile; fileRef = 377D44F21D247D0A004E35CB /* RCTAppleHealthKit+Methods_Characteristic.m */; };
37837E7D1DCFE270000201A0 /* RCTAppleHealthKit+Methods_Sleep.m in Sources */ = {isa = PBXBuildFile; fileRef = 37837E7C1DCFE270000201A0 /* RCTAppleHealthKit+Methods_Sleep.m */; };
58C81E6F1F84F6970005DD48 /* RCTAppleHealthKit+Methods_Activity.m in Sources */ = {isa = PBXBuildFile; fileRef = 58C81E6D1F84F6970005DD48 /* RCTAppleHealthKit+Methods_Activity.m */; };
61232F931E303758000A5026 /* RCTAppleHealthKit+Methods_Mindfulness.m in Sources */ = {isa = PBXBuildFile; fileRef = 61232F921E303758000A5026 /* RCTAppleHealthKit+Methods_Mindfulness.m */; };
64C42D4A1D351A8800A0A8F7 /* RCTAppleHealthKit+Methods_Vitals.m in Sources */ = {isa = PBXBuildFile; fileRef = 64C42D491D351A8800A0A8F7 /* RCTAppleHealthKit+Methods_Vitals.m */; };
64E0E73F1D37947B00EAB905 /* RCTAppleHealthKit+Methods_Results.m in Sources */ = {isa = PBXBuildFile; fileRef = 64E0E73E1D37947B00EAB905 /* RCTAppleHealthKit+Methods_Results.m */; };
......@@ -53,6 +54,8 @@
377D44F21D247D0A004E35CB /* RCTAppleHealthKit+Methods_Characteristic.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "RCTAppleHealthKit+Methods_Characteristic.m"; sourceTree = "<group>"; };
37837E7B1DCFE270000201A0 /* RCTAppleHealthKit+Methods_Sleep.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "RCTAppleHealthKit+Methods_Sleep.h"; sourceTree = "<group>"; };
37837E7C1DCFE270000201A0 /* RCTAppleHealthKit+Methods_Sleep.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "RCTAppleHealthKit+Methods_Sleep.m"; sourceTree = "<group>"; };
58C81E6D1F84F6970005DD48 /* RCTAppleHealthKit+Methods_Activity.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "RCTAppleHealthKit+Methods_Activity.m"; sourceTree = "<group>"; };
58C81E6E1F84F6970005DD48 /* RCTAppleHealthKit+Methods_Activity.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "RCTAppleHealthKit+Methods_Activity.h"; sourceTree = "<group>"; };
61232F911E303758000A5026 /* RCTAppleHealthKit+Methods_Mindfulness.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "RCTAppleHealthKit+Methods_Mindfulness.h"; sourceTree = "<group>"; };
61232F921E303758000A5026 /* RCTAppleHealthKit+Methods_Mindfulness.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "RCTAppleHealthKit+Methods_Mindfulness.m"; sourceTree = "<group>"; };
64C42D481D351A8800A0A8F7 /* RCTAppleHealthKit+Methods_Vitals.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "RCTAppleHealthKit+Methods_Vitals.h"; sourceTree = "<group>"; };
......@@ -93,6 +96,8 @@
3774C88F1D2092F20000B3F3 /* RCTAppleHealthKit */ = {
isa = PBXGroup;
children = (
58C81E6E1F84F6970005DD48 /* RCTAppleHealthKit+Methods_Activity.h */,
58C81E6D1F84F6970005DD48 /* RCTAppleHealthKit+Methods_Activity.m */,
61232F911E303758000A5026 /* RCTAppleHealthKit+Methods_Mindfulness.h */,
61232F921E303758000A5026 /* RCTAppleHealthKit+Methods_Mindfulness.m */,
64E0E73D1D37947B00EAB905 /* RCTAppleHealthKit+Methods_Results.h */,
......@@ -189,6 +194,7 @@
83C70EFC1FDFA6BB00AD7AA9 /* RCTAppleHealthKit+NSUserDefaults.m in Sources */,
3774C89E1D2095850000B3F3 /* RCTAppleHealthKit+TypesAndPermissions.m in Sources */,
3774C8D71D20C65F0000B3F3 /* RCTAppleHealthKit+Methods_Fitness.m in Sources */,
58C81E6F1F84F6970005DD48 /* RCTAppleHealthKit+Methods_Activity.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
......
//
// RCTAppleHealthKit+Methods_Activity.h
// RCTAppleHealthKit
//
// Created by Alexander Vallorosi on 4/27/17.
// This source code is licensed under the MIT-style license found in the
// LICENSE file in the root directory of this source tree.
//
#import "RCTAppleHealthKit.h"
@interface RCTAppleHealthKit (Methods_Activity)
- (void)activity_getActiveEnergyBurned:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)activity_getBasalEnergyBurned:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
@end
//
// RCTAppleHealthKit+Methods_Activity.m
// RCTAppleHealthKit
//
// Created by Alexander Vallorosi on 4/27/17.
// This source code is licensed under the MIT-style license found in the
// LICENSE file in the root directory of this source tree.
#import "RCTAppleHealthKit+Methods_Activity.h"
#import "RCTAppleHealthKit+Queries.h"
#import "RCTAppleHealthKit+Utils.h"
@implementation RCTAppleHealthKit (Methods_Activity)
- (void)activity_getActiveEnergyBurned:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback
{
HKQuantityType *activeEnergyType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierActiveEnergyBurned];
NSDate *startDate = [RCTAppleHealthKit dateFromOptions:input key:@"startDate" withDefault:nil];
NSDate *endDate = [RCTAppleHealthKit dateFromOptions:input key:@"endDate" withDefault:[NSDate date]];
HKUnit *cal = [HKUnit kilocalorieUnit];
if(startDate == nil){
callback(@[RCTMakeError(@"startDate is required in options", nil, nil)]);
return;
}
NSPredicate * predicate = [RCTAppleHealthKit predicateForSamplesBetweenDates:startDate endDate:endDate];
[self fetchQuantitySamplesOfType:activeEnergyType
unit:cal
predicate:predicate
ascending:false
limit:HKObjectQueryNoLimit
completion:^(NSArray *results, NSError *error) {
if(results){
callback(@[[NSNull null], results]);
return;
} else {
callback(@[RCTJSErrorFromNSError(error)]);
return;
}
}];
}
- (void)activity_getBasalEnergyBurned:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback
{
HKQuantityType *basalEnergyType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierBasalEnergyBurned];
NSDate *startDate = [RCTAppleHealthKit dateFromOptions:input key:@"startDate" withDefault:nil];
NSDate *endDate = [RCTAppleHealthKit dateFromOptions:input key:@"endDate" withDefault:[NSDate date]];
HKUnit *cal = [HKUnit kilocalorieUnit];
if(startDate == nil){
callback(@[RCTMakeError(@"startDate is required in options", nil, nil)]);
return;
}
NSPredicate * predicate = [RCTAppleHealthKit predicateForSamplesBetweenDates:startDate endDate:endDate];
[self fetchQuantitySamplesOfType:basalEnergyType
unit:cal
predicate:predicate
ascending:false
limit:HKObjectQueryNoLimit
completion:^(NSArray *results, NSError *error) {
if(results){
callback(@[[NSNull null], results]);
return;
} else {
callback(@[RCTJSErrorFromNSError(error)]);
return;
}
}];
}
@end
......@@ -3,7 +3,8 @@
// RCTAppleHealthKit
//
// Created by Greg Wilson on 2016-06-26.
// Copyright © 2016 Greg Wilson. All rights reserved.
// This source code is licensed under the MIT-style license found in the
// LICENSE file in the root directory of this source tree.
//
#import "RCTAppleHealthKit.h"
......@@ -22,6 +23,11 @@
- (void)body_saveHeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)body_getLatestBodyFatPercentage:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)body_getBodyFatPercentageSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)body_saveBodyFatPercentage:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)body_getLatestLeanBodyMass:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)body_getLeanBodyMassSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)body_saveLeanBodyMass:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
@end
......@@ -3,7 +3,8 @@
// RCTAppleHealthKit
//
// Created by Greg Wilson on 2016-06-26.
// Copyright © 2016 Greg Wilson. All rights reserved.
// This source code is licensed under the MIT-style license found in the
// LICENSE file in the root directory of this source tree.
//
#import "RCTAppleHealthKit+Methods_Body.h"
......@@ -17,22 +18,17 @@
{
HKQuantityType *weightType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMass];
HKUnit *unit = [RCTAppleHealthKit hkUnitFromOptions:input];
if(unit == nil){
unit = [HKUnit poundUnit];
}
HKUnit *unit = [RCTAppleHealthKit hkUnitFromOptions:input key:@"unit" withDefault:[HKUnit poundUnit]];
[self fetchMostRecentQuantitySampleOfType:weightType
predicate:nil
completion:^(HKQuantity *mostRecentQuantity, NSDate *startDate, NSDate *endDate, NSError *error) {
if (!mostRecentQuantity) {
NSLog(@"error getting latest weight: %@", error);
callback(@[RCTMakeError(@"error getting latest weight", error, nil)]);
callback(@[RCTJSErrorFromNSError(error)]);
}
else {
// Determine the weight in the required unit.
double usersWeight = [mostRecentQuantity doubleValueForUnit:unit];
NSDictionary *response = @{
@"value" : @(usersWeight),
@"startDate" : [RCTAppleHealthKit buildISO8601StringFromDate:startDate],
......@@ -70,8 +66,7 @@
callback(@[[NSNull null], results]);
return;
} else {
NSLog(@"error getting weight samples: %@", error);
callback(@[RCTMakeError(@"error getting weight samples", nil, nil)]);
callback(@[RCTJSErrorFromNSError(error)]);
return;
}
}];
......@@ -81,7 +76,7 @@
- (void)body_saveWeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback
{
double weight = [RCTAppleHealthKit doubleValueFromOptions:input];
NSDate *sampleDate = [RCTAppleHealthKit dateFromOptionsDefaultNow:input];
NSDate *sampleDate = [RCTAppleHealthKit dateFromOptions:input key:@"startDate" withDefault:[NSDate date]];
HKUnit *unit = [RCTAppleHealthKit hkUnitFromOptions:input key:@"unit" withDefault:[HKUnit poundUnit]];
HKQuantity *weightQuantity = [HKQuantity quantityWithUnit:unit doubleValue:weight];
......@@ -90,8 +85,7 @@
[self.healthStore saveObject:weightSample withCompletion:^(BOOL success, NSError *error) {
if (!success) {
NSLog(@"error saving the weight sample: %@", error);
callback(@[RCTMakeError(@"error saving the weight sample", error, nil)]);
callback(@[RCTJSErrorFromNSError(error)]);
return;
}
callback(@[[NSNull null], @(weight)]);
......@@ -107,8 +101,7 @@
predicate:nil
completion:^(HKQuantity *mostRecentQuantity, NSDate *startDate, NSDate *endDate, NSError *error) {
if (!mostRecentQuantity) {
NSLog(@"error getting latest BMI: %@", error);
callback(@[RCTMakeError(@"error getting latest BMI", error, nil)]);
callback(@[RCTJSErrorFromNSError(error)]);
}
else {
// Determine the bmi in the required unit.
......@@ -139,8 +132,7 @@
[self.healthStore saveObject:bmiSample withCompletion:^(BOOL success, NSError *error) {
if (!success) {
NSLog(@"error saving BMI sample: %@.", error);
callback(@[RCTMakeError(@"error saving BMI sample", error, nil)]);
callback(@[RCTJSErrorFromNSError(error)]);
return;
}
callback(@[[NSNull null], @(bmi)]);
......@@ -151,11 +143,7 @@
- (void)body_getLatestHeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback
{
HKQuantityType *heightType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeight];
HKUnit *unit = [RCTAppleHealthKit hkUnitFromOptions:input];
if(unit == nil){
unit = [HKUnit inchUnit];
}
HKUnit *unit = [RCTAppleHealthKit hkUnitFromOptions:input key:@"unit" withDefault:[HKUnit inchUnit]];;
[self fetchMostRecentQuantitySampleOfType:heightType
predicate:nil
......@@ -205,8 +193,7 @@
callback(@[[NSNull null], results]);
return;
} else {
NSLog(@"error getting height samples: %@", error);
callback(@[RCTMakeError(@"error getting height samples", error, nil)]);
callback(@[RCTJSErrorFromNSError(error)]);
return;
}
}];
......@@ -217,11 +204,7 @@
{
double height = [RCTAppleHealthKit doubleValueFromOptions:input];
NSDate *sampleDate = [RCTAppleHealthKit dateFromOptionsDefaultNow:input];
HKUnit *heightUnit = [RCTAppleHealthKit hkUnitFromOptions:input];
if(heightUnit == nil){
heightUnit = [HKUnit inchUnit];
}
HKUnit *heightUnit = [RCTAppleHealthKit hkUnitFromOptions:input key:@"unit" withDefault:[HKUnit inchUnit]];
HKQuantity *heightQuantity = [HKQuantity quantityWithUnit:heightUnit doubleValue:height];
HKQuantityType *heightType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeight];
......@@ -229,8 +212,7 @@
[self.healthStore saveObject:heightSample withCompletion:^(BOOL success, NSError *error) {
if (!success) {
NSLog(@"error saving height sample: %@", error);
callback(@[RCTMakeError(@"error saving height sample", error, nil)]);
callback(@[RCTJSErrorFromNSError(error)]);
return;
}
callback(@[[NSNull null], @(height)]);
......@@ -246,8 +228,7 @@
predicate:nil
completion:^(HKQuantity *mostRecentQuantity, NSDate *startDate, NSDate *endDate, NSError *error) {
if (!mostRecentQuantity) {
NSLog(@"error getting latest body fat percentage: %@", error);
callback(@[RCTMakeError(@"error getting latest body fat percentage", error, nil)]);
callback(@[RCTJSErrorFromNSError(error)]);
}
else {
// Determine the weight in the required unit.
......@@ -268,6 +249,62 @@
}
- (void)body_getBodyFatPercentageSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback
{
HKQuantityType *bodyFatPercentType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyFatPercentage];
HKUnit *unit = [HKUnit percentUnit];
NSUInteger limit = [RCTAppleHealthKit uintFromOptions:input key:@"limit" withDefault:HKObjectQueryNoLimit];
BOOL ascending = [RCTAppleHealthKit boolFromOptions:input key:@"ascending" withDefault:false];
NSDate *startDate = [RCTAppleHealthKit dateFromOptions:input key:@"startDate" withDefault:nil];
NSDate *endDate = [RCTAppleHealthKit dateFromOptions:input key:@"endDate" withDefault:[NSDate date]];
if(startDate == nil){
callback(@[RCTMakeError(@"startDate is required in options", nil, nil)]);
return;
}
NSPredicate * predicate = [RCTAppleHealthKit predicateForSamplesBetweenDates:startDate endDate:endDate];
[self fetchQuantitySamplesOfType:bodyFatPercentType
unit:unit
predicate:predicate
ascending:ascending
limit:limit
completion:^(NSArray *results, NSError *error) {
if(results){
callback(@[[NSNull null], results]);
return;
} else {
NSLog(@"error getting body fat percentage samples: %@", error);
callback(@[RCTMakeError(@"error getting body fat percentage samples", nil, nil)]);
return;
}
}];
}
- (void)body_saveBodyFatPercentage:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback
{
double percentage = [RCTAppleHealthKit doubleValueFromOptions:input];
NSDate *sampleDate = [RCTAppleHealthKit dateFromOptionsDefaultNow:input];
HKUnit *unit = [HKUnit percentUnit];
percentage = percentage / 100;
HKQuantity *bodyFatPercentQuantity = [HKQuantity quantityWithUnit:unit doubleValue:percentage];
HKQuantityType *bodyFatPercentType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyFatPercentage];
HKQuantitySample *bodyFatPercentSample = [HKQuantitySample quantitySampleWithType:bodyFatPercentType quantity:bodyFatPercentQuantity startDate:sampleDate endDate:sampleDate];
[self.healthStore saveObject:bodyFatPercentSample withCompletion:^(BOOL success, NSError *error) {
if (!success) {
NSLog(@"error saving body fat percent sample: %@", error);
callback(@[RCTMakeError(@"error saving body fat percent sample", error, nil)]);
return;
}
callback(@[[NSNull null], @(percentage)]);
}];
}
- (void)body_getLatestLeanBodyMass:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback
{
HKQuantityType *leanBodyMassType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierLeanBodyMass];
......@@ -276,8 +313,7 @@
predicate:nil
completion:^(HKQuantity *mostRecentQuantity, NSDate *startDate, NSDate *endDate, NSError *error) {
if (!mostRecentQuantity) {
NSLog(@"error getting latest lean body mass: %@", error);
callback(@[RCTMakeError(@"error getting latest lean body mass", error, nil)]);
callback(@[RCTJSErrorFromNSError(error)]);
}
else {
HKUnit *weightUnit = [HKUnit poundUnit];
......@@ -294,4 +330,58 @@
}];
}
- (void)body_getLeanBodyMassSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback
{
HKQuantityType *leanBodyMassType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierLeanBodyMass];
HKUnit *unit = [RCTAppleHealthKit hkUnitFromOptions:input key:@"unit" withDefault:[HKUnit poundUnit]];
NSUInteger limit = [RCTAppleHealthKit uintFromOptions:input key:@"limit" withDefault:HKObjectQueryNoLimit];
BOOL ascending = [RCTAppleHealthKit boolFromOptions:input key:@"ascending" withDefault:false];
NSDate *startDate = [RCTAppleHealthKit dateFromOptions:input key:@"startDate" withDefault:nil];
NSDate *endDate = [RCTAppleHealthKit dateFromOptions:input key:@"endDate" withDefault:[NSDate date]];
if(startDate == nil){
callback(@[RCTMakeError(@"startDate is required in options", nil, nil)]);
return;
}
NSPredicate * predicate = [RCTAppleHealthKit predicateForSamplesBetweenDates:startDate endDate:endDate];
[self fetchQuantitySamplesOfType:leanBodyMassType
unit:unit
predicate:predicate
ascending:ascending
limit:limit
completion:^(NSArray *results, NSError *error) {
if(results){
callback(@[[NSNull null], results]);
return;
} else {
NSLog(@"error getting lean body mass samples: %@", error);
callback(@[RCTMakeError(@"error getting lean body mass samples", nil, nil)]);
return;
}
}];
}
- (void)body_saveLeanBodyMass:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback
{
double mass = [RCTAppleHealthKit doubleValueFromOptions:input];
NSDate *sampleDate = [RCTAppleHealthKit dateFromOptions:input key:@"startDate" withDefault:[NSDate date]];
HKUnit *unit = [RCTAppleHealthKit hkUnitFromOptions:input key:@"unit" withDefault:[HKUnit poundUnit]];
HKQuantity *massQuantity = [HKQuantity quantityWithUnit:unit doubleValue:mass];
HKQuantityType *massType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierLeanBodyMass];
HKQuantitySample *massSample = [HKQuantitySample quantitySampleWithType:massType quantity:massQuantity startDate:sampleDate endDate:sampleDate];
[self.healthStore saveObject:massSample withCompletion:^(BOOL success, NSError *error) {
if (!success) {
NSLog(@"error saving lean body mass sample: %@", error);
callback(@[RCTMakeError(@"error saving lean body mass sample", error, nil)]);
return;
}
callback(@[[NSNull null], @(mass)]);
}];
}
@end
......@@ -3,7 +3,8 @@
// RCTAppleHealthKit
//
// Created by Greg Wilson on 2016-06-29.
// Copyright © 2016 Greg Wilson. All rights reserved.
// This source code is licensed under the MIT-style license found in the
// LICENSE file in the root directory of this source tree.
//
#import "RCTAppleHealthKit.h"
......
......@@ -3,7 +3,8 @@
// RCTAppleHealthKit
//
// Created by Greg Wilson on 2016-06-29.
// Copyright © 2016 Greg Wilson. All rights reserved.
// This source code is licensed under the MIT-style license found in the
// LICENSE file in the root directory of this source tree.
//
#import "RCTAppleHealthKit+Methods_Characteristic.h"
......@@ -33,8 +34,7 @@
}
if(value == nil){
NSLog(@"error getting biological sex: %@", error);
callback(@[RCTMakeError(@"error getting biological sex", error, nil)]);
callback(@[RCTJSErrorFromNSError(error)]);
return;
}
......@@ -51,8 +51,7 @@
NSDate *dob = [self.healthStore dateOfBirthWithError:&error];
if(error != nil){
NSLog(@"error getting date of birth: %@", error);
callback(@[RCTMakeError(@"error getting date of birth", error, nil)]);
callback(@[RCTJSErrorFromNSError(error)]);
return;
}
if(dob == nil) {
......
//
// RCTAppleHealthKit+Methods_Dietary.h
// RCTAppleHealthKit
//
// Created by Greg Wilson on 2016-06-26.
// Copyright © 2016 Greg Wilson. All rights reserved.
//
#import "RCTAppleHealthKit.h"
@interface RCTAppleHealthKit (Methods_Dietary)
- (void)saveFood:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)saveWater:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
@end
//
// RCTAppleHealthKit+Methods_Dietary.m
// RCTAppleHealthKit
//
// Created by Greg Wilson on 2016-06-26.
// Copyright © 2016 Greg Wilson. All rights reserved.
//
#import "RCTAppleHealthKit+Methods_Dietary.h"
#import "RCTAppleHealthKit+Queries.h"
#import "RCTAppleHealthKit+Utils.h"
#import <React/RCTBridgeModule.h>
#import <React/RCTEventDispatcher.h>
@implementation RCTAppleHealthKit (Methods_Dietary)
- (void)saveFood:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback
{
NSString *foodNameValue = [RCTAppleHealthKit stringFromOptions:input key:@"foodName" withDefault:nil];
NSString *mealNameValue = [RCTAppleHealthKit stringFromOptions:input key:@"mealType" withDefault:nil];
NSDate *timeFoodWasConsumed = [RCTAppleHealthKit dateFromOptions:input key:@"date" withDefault:[NSDate date]];
double biotinValue = [RCTAppleHealthKit doubleFromOptions:input key:@"biotin" withDefault:(double)0];
double caffeineValue = [RCTAppleHealthKit doubleFromOptions:input key:@"caffeine" withDefault:(double)0];
double calciumValue = [RCTAppleHealthKit doubleFromOptions:input key:@"calcium" withDefault:(double)0];
double carbohydratesValue = [RCTAppleHealthKit doubleFromOptions:input key:@"carbohydrates" withDefault:(double)0];
double chlorideValue = [RCTAppleHealthKit doubleFromOptions:input key:@"chloride" withDefault:(double)0];
double cholesterolValue = [RCTAppleHealthKit doubleFromOptions:input key:@"cholesterol" withDefault:(double)0];
double copperValue = [RCTAppleHealthKit doubleFromOptions:input key:@"copper" withDefault:(double)0];
double energyConsumedValue = [RCTAppleHealthKit doubleFromOptions:input key:@"energy" withDefault:(double)0];
double fatMonounsaturatedValue = [RCTAppleHealthKit doubleFromOptions:input key:@"fatMonounsaturated" withDefault:(double)0];
double fatPolyunsaturatedValue = [RCTAppleHealthKit doubleFromOptions:input key:@"fatPolyunsaturated" withDefault:(double)0];
double fatSaturatedValue = [RCTAppleHealthKit doubleFromOptions:input key:@"fatSaturated" withDefault:(double)0];
double fatTotalValue = [RCTAppleHealthKit doubleFromOptions:input key:@"fatTotal" withDefault:(double)0];
double fiberValue = [RCTAppleHealthKit doubleFromOptions:input key:@"fiber" withDefault:(double)0];
double folateValue = [RCTAppleHealthKit doubleFromOptions:input key:@"folate" withDefault:(double)0];
double iodineValue = [RCTAppleHealthKit doubleFromOptions:input key:@"iodine" withDefault:(double)0];
double ironValue = [RCTAppleHealthKit doubleFromOptions:input key:@"iron" withDefault:(double)0];
double magnesiumValue = [RCTAppleHealthKit doubleFromOptions:input key:@"magnesium" withDefault:(double)0];
double manganeseValue = [RCTAppleHealthKit doubleFromOptions:input key:@"manganese" withDefault:(double)0];
double molybdenumValue = [RCTAppleHealthKit doubleFromOptions:input key:@"molybdenum" withDefault:(double)0];
double niacinValue = [RCTAppleHealthKit doubleFromOptions:input key:@"niacin" withDefault:(double)0];
double pantothenicAcidValue = [RCTAppleHealthKit doubleFromOptions:input key:@"pantothenicAcid" withDefault:(double)0];
double phosphorusValue = [RCTAppleHealthKit doubleFromOptions:input key:@"phosphorus" withDefault:(double)0];
double potassiumValue = [RCTAppleHealthKit doubleFromOptions:input key:@"potassium" withDefault:(double)0];
double proteinValue = [RCTAppleHealthKit doubleFromOptions:input key:@"protein" withDefault:(double)0];
double riboflavinValue = [RCTAppleHealthKit doubleFromOptions:input key:@"riboflavin" withDefault:(double)0];
double seleniumValue = [RCTAppleHealthKit doubleFromOptions:input key:@"selenium" withDefault:(double)0];
double sodiumValue = [RCTAppleHealthKit doubleFromOptions:input key:@"sodium" withDefault:(double)0];
double sugarValue = [RCTAppleHealthKit doubleFromOptions:input key:@"sugar" withDefault:(double)0];
double thiaminValue = [RCTAppleHealthKit doubleFromOptions:input key:@"thiamin" withDefault:(double)0];
double vitaminAValue = [RCTAppleHealthKit doubleFromOptions:input key:@"vitaminA" withDefault:(double)0];
double vitaminB12Value = [RCTAppleHealthKit doubleFromOptions:input key:@"vitaminB12" withDefault:(double)0];
double vitaminB6Value = [RCTAppleHealthKit doubleFromOptions:input key:@"vitaminB6" withDefault:(double)0];
double vitaminCValue = [RCTAppleHealthKit doubleFromOptions:input key:@"vitaminC" withDefault:(double)0];
double vitaminDValue = [RCTAppleHealthKit doubleFromOptions:input key:@"vitaminD" withDefault:(double)0];
double vitaminEValue = [RCTAppleHealthKit doubleFromOptions:input key:@"vitaminE" withDefault:(double)0];
double vitaminKValue = [RCTAppleHealthKit doubleFromOptions:input key:@"vitaminK" withDefault:(double)0];
double zincValue = [RCTAppleHealthKit doubleFromOptions:input key:@"zinc" withDefault:(double)0];
// Metadata including some new food-related keys //
NSDictionary *metadata = @{
HKMetadataKeyFoodType:foodNameValue,
//@"HKFoodBrandName":@"FoodBrandName", // Restaurant name or packaged food brand name
//@"HKFoodTypeUUID":@"FoodTypeUUID", // Identifier for this food
@"HKFoodMeal":mealNameValue//, // Breakfast, Lunch, Dinner, or Snacks
//@"HKFoodImageName":@"FoodImageName" // Food icon name
};
// Create nutrtional data for food //
NSMutableSet *mySet = [[NSMutableSet alloc] init];
if (biotinValue > 0){
HKQuantitySample* biotin = [HKQuantitySample quantitySampleWithType:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryBiotin]
quantity:[HKQuantity quantityWithUnit:[HKUnit gramUnit] doubleValue:biotinValue]
startDate:timeFoodWasConsumed
endDate:timeFoodWasConsumed
metadata:metadata];
[mySet addObject:biotin];
}
if (caffeineValue > 0){
HKQuantitySample* caffeine = [HKQuantitySample quantitySampleWithType:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryCaffeine]
quantity:[HKQuantity quantityWithUnit:[HKUnit gramUnit] doubleValue:caffeineValue]
startDate:timeFoodWasConsumed
endDate:timeFoodWasConsumed
metadata:metadata];
[mySet addObject:caffeine];
}
if (calciumValue > 0){
HKQuantitySample* calcium = [HKQuantitySample quantitySampleWithType:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryCalcium]
quantity:[HKQuantity quantityWithUnit:[HKUnit gramUnit] doubleValue:calciumValue]
startDate:timeFoodWasConsumed
endDate:timeFoodWasConsumed
metadata:metadata];
[mySet addObject:calcium];
}
if (carbohydratesValue > 0){
HKQuantitySample* carbohydrates = [HKQuantitySample quantitySampleWithType:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryCarbohydrates]
quantity:[HKQuantity quantityWithUnit:[HKUnit gramUnit] doubleValue:carbohydratesValue]
startDate:timeFoodWasConsumed
endDate:timeFoodWasConsumed
metadata:metadata];
[mySet addObject:carbohydrates];
}
if (chlorideValue > 0){
HKQuantitySample* chloride = [HKQuantitySample quantitySampleWithType:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryChloride]
quantity:[HKQuantity quantityWithUnit:[HKUnit gramUnit] doubleValue:chlorideValue]
startDate:timeFoodWasConsumed
endDate:timeFoodWasConsumed
metadata:metadata];
[mySet addObject:chloride];
}
if (cholesterolValue > 0){
HKQuantitySample* cholesterol = [HKQuantitySample quantitySampleWithType:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryCholesterol]
quantity:[HKQuantity quantityWithUnit:[HKUnit gramUnit] doubleValue:cholesterolValue]
startDate:timeFoodWasConsumed
endDate:timeFoodWasConsumed
metadata:metadata];
[mySet addObject:cholesterol];
}
if (copperValue > 0){
HKQuantitySample* copper = [HKQuantitySample quantitySampleWithType:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryCopper]
quantity:[HKQuantity quantityWithUnit:[HKUnit gramUnit] doubleValue:copperValue]
startDate:timeFoodWasConsumed
endDate:timeFoodWasConsumed
metadata:metadata];
[mySet addObject:copper];
}
if (energyConsumedValue > 0){
HKQuantitySample* energyConsumed = [HKQuantitySample quantitySampleWithType:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryEnergyConsumed]
quantity:[HKQuantity quantityWithUnit:[HKUnit kilocalorieUnit] doubleValue:energyConsumedValue]
startDate:timeFoodWasConsumed
endDate:timeFoodWasConsumed
metadata:metadata];
[mySet addObject:energyConsumed];
}
if (fatMonounsaturatedValue > 0){
HKQuantitySample* fatMonounsaturated = [HKQuantitySample quantitySampleWithType:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryFatMonounsaturated]
quantity:[HKQuantity quantityWithUnit:[HKUnit gramUnit] doubleValue:fatMonounsaturatedValue]
startDate:timeFoodWasConsumed
endDate:timeFoodWasConsumed
metadata:metadata];
[mySet addObject:fatMonounsaturated];
}
if (fatPolyunsaturatedValue > 0){
HKQuantitySample* fatPolyunsaturated = [HKQuantitySample quantitySampleWithType:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryFatPolyunsaturated]
quantity:[HKQuantity quantityWithUnit:[HKUnit gramUnit] doubleValue:fatPolyunsaturatedValue]
startDate:timeFoodWasConsumed
endDate:timeFoodWasConsumed
metadata:metadata];
[mySet addObject:fatPolyunsaturated];
}
if (fatSaturatedValue > 0){
HKQuantitySample* fatSaturated = [HKQuantitySample quantitySampleWithType:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryFatSaturated]
quantity:[HKQuantity quantityWithUnit:[HKUnit gramUnit] doubleValue:fatSaturatedValue]
startDate:timeFoodWasConsumed
endDate:timeFoodWasConsumed
metadata:metadata];
[mySet addObject:fatSaturated];
}
if (fatTotalValue > 0){
HKQuantitySample* fatTotal = [HKQuantitySample quantitySampleWithType:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryFatTotal]
quantity:[HKQuantity quantityWithUnit:[HKUnit gramUnit] doubleValue:fatTotalValue]
startDate:timeFoodWasConsumed
endDate:timeFoodWasConsumed
metadata:metadata];
[mySet addObject:fatTotal];
}
if (fiberValue > 0){
HKQuantitySample* fiber = [HKQuantitySample quantitySampleWithType:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryFiber]
quantity:[HKQuantity quantityWithUnit:[HKUnit gramUnit] doubleValue:fiberValue]
startDate:timeFoodWasConsumed
endDate:timeFoodWasConsumed
metadata:metadata];
[mySet addObject:fiber];
}
if (folateValue > 0){
HKQuantitySample* folate = [HKQuantitySample quantitySampleWithType:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryFolate]
quantity:[HKQuantity quantityWithUnit:[HKUnit gramUnit] doubleValue:folateValue]
startDate:timeFoodWasConsumed
endDate:timeFoodWasConsumed
metadata:metadata];
[mySet addObject:folate];
}
if (iodineValue > 0){
HKQuantitySample* iodine = [HKQuantitySample quantitySampleWithType:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryIodine]
quantity:[HKQuantity quantityWithUnit:[HKUnit gramUnit] doubleValue:iodineValue]
startDate:timeFoodWasConsumed
endDate:timeFoodWasConsumed
metadata:metadata];
[mySet addObject:iodine];
}
if (ironValue > 0){
HKQuantitySample* iron = [HKQuantitySample quantitySampleWithType:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryIron]
quantity:[HKQuantity quantityWithUnit:[HKUnit gramUnit] doubleValue:ironValue]
startDate:timeFoodWasConsumed
endDate:timeFoodWasConsumed
metadata:metadata];
[mySet addObject:iron];
}
if (magnesiumValue > 0){
HKQuantitySample* magnesium = [HKQuantitySample quantitySampleWithType:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryMagnesium]
quantity:[HKQuantity quantityWithUnit:[HKUnit gramUnit] doubleValue:magnesiumValue]
startDate:timeFoodWasConsumed
endDate:timeFoodWasConsumed
metadata:metadata];
[mySet addObject:magnesium];
}
if (manganeseValue > 0){
HKQuantitySample* manganese = [HKQuantitySample quantitySampleWithType:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryManganese]
quantity:[HKQuantity quantityWithUnit:[HKUnit gramUnit] doubleValue:manganeseValue]
startDate:timeFoodWasConsumed
endDate:timeFoodWasConsumed
metadata:metadata];
[mySet addObject:manganese];
}
if (molybdenumValue > 0){
HKQuantitySample* molybdenum = [HKQuantitySample quantitySampleWithType:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryMolybdenum]
quantity:[HKQuantity quantityWithUnit:[HKUnit gramUnit] doubleValue:molybdenumValue]
startDate:timeFoodWasConsumed
endDate:timeFoodWasConsumed
metadata:metadata];
[mySet addObject:molybdenum];
}
if (niacinValue > 0){
HKQuantitySample* niacin = [HKQuantitySample quantitySampleWithType:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryNiacin]
quantity:[HKQuantity quantityWithUnit:[HKUnit gramUnit] doubleValue:niacinValue]
startDate:timeFoodWasConsumed
endDate:timeFoodWasConsumed
metadata:metadata];
[mySet addObject:niacin];
}
if (pantothenicAcidValue > 0){
HKQuantitySample* pantothenicAcid = [HKQuantitySample quantitySampleWithType:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryPantothenicAcid]
quantity:[HKQuantity quantityWithUnit:[HKUnit gramUnit] doubleValue:pantothenicAcidValue]
startDate:timeFoodWasConsumed
endDate:timeFoodWasConsumed
metadata:metadata];
[mySet addObject:pantothenicAcid];
}
if (phosphorusValue > 0){
HKQuantitySample* phosphorus = [HKQuantitySample quantitySampleWithType:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryPhosphorus]
quantity:[HKQuantity quantityWithUnit:[HKUnit gramUnit] doubleValue:phosphorusValue]
startDate:timeFoodWasConsumed
endDate:timeFoodWasConsumed
metadata:metadata];
[mySet addObject:phosphorus];
}
if (potassiumValue > 0){
HKQuantitySample* potassium = [HKQuantitySample quantitySampleWithType:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryPotassium]
quantity:[HKQuantity quantityWithUnit:[HKUnit gramUnit] doubleValue:potassiumValue]
startDate:timeFoodWasConsumed
endDate:timeFoodWasConsumed
metadata:metadata];
[mySet addObject:potassium];
}
if (proteinValue > 0){
HKQuantitySample* protein = [HKQuantitySample quantitySampleWithType:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryProtein]
quantity:[HKQuantity quantityWithUnit:[HKUnit gramUnit] doubleValue:proteinValue]
startDate:timeFoodWasConsumed
endDate:timeFoodWasConsumed
metadata:metadata];
[mySet addObject:protein];
}
if (riboflavinValue > 0){
HKQuantitySample* riboflavin = [HKQuantitySample quantitySampleWithType:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryRiboflavin]
quantity:[HKQuantity quantityWithUnit:[HKUnit gramUnit] doubleValue:riboflavinValue]
startDate:timeFoodWasConsumed
endDate:timeFoodWasConsumed
metadata:metadata];
[mySet addObject:riboflavin];
}
if (seleniumValue > 0){
HKQuantitySample* selenium = [HKQuantitySample quantitySampleWithType:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietarySelenium]
quantity:[HKQuantity quantityWithUnit:[HKUnit gramUnit] doubleValue:seleniumValue]
startDate:timeFoodWasConsumed
endDate:timeFoodWasConsumed
metadata:metadata];
[mySet addObject:selenium];
}
if (sodiumValue > 0){
HKQuantitySample* sodium = [HKQuantitySample quantitySampleWithType:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietarySodium]
quantity:[HKQuantity quantityWithUnit:[HKUnit gramUnit] doubleValue:sodiumValue]
startDate:timeFoodWasConsumed
endDate:timeFoodWasConsumed
metadata:metadata];
[mySet addObject:sodium];
}
if (sugarValue > 0){
HKQuantitySample* sugar = [HKQuantitySample quantitySampleWithType:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietarySugar]
quantity:[HKQuantity quantityWithUnit:[HKUnit gramUnit] doubleValue:sugarValue]
startDate:timeFoodWasConsumed
endDate:timeFoodWasConsumed
metadata:metadata];
[mySet addObject:sugar];
}
if (thiaminValue > 0){
HKQuantitySample* thiamin = [HKQuantitySample quantitySampleWithType:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryThiamin]
quantity:[HKQuantity quantityWithUnit:[HKUnit gramUnit] doubleValue:thiaminValue]
startDate:timeFoodWasConsumed
endDate:timeFoodWasConsumed
metadata:metadata];
[mySet addObject:thiamin];
}
if (vitaminAValue > 0){
HKQuantitySample* vitaminA = [HKQuantitySample quantitySampleWithType:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryVitaminA]
quantity:[HKQuantity quantityWithUnit:[HKUnit gramUnit] doubleValue:vitaminAValue]
startDate:timeFoodWasConsumed
endDate:timeFoodWasConsumed
metadata:metadata];
[mySet addObject:vitaminA];
}
if (vitaminB12Value > 0){
HKQuantitySample* vitaminB12 = [HKQuantitySample quantitySampleWithType:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryVitaminB12]
quantity:[HKQuantity quantityWithUnit:[HKUnit gramUnit] doubleValue:vitaminB12Value]
startDate:timeFoodWasConsumed
endDate:timeFoodWasConsumed
metadata:metadata];
[mySet addObject:vitaminB12];
}
if (vitaminB6Value > 0){
HKQuantitySample* vitaminB6 = [HKQuantitySample quantitySampleWithType:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryVitaminB6]
quantity:[HKQuantity quantityWithUnit:[HKUnit gramUnit] doubleValue:vitaminB6Value]
startDate:timeFoodWasConsumed
endDate:timeFoodWasConsumed
metadata:metadata];
[mySet addObject:vitaminB6];
}
if (vitaminCValue > 0){
HKQuantitySample* vitaminC = [HKQuantitySample quantitySampleWithType:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryVitaminC]
quantity:[HKQuantity quantityWithUnit:[HKUnit gramUnit] doubleValue:vitaminCValue]
startDate:timeFoodWasConsumed
endDate:timeFoodWasConsumed
metadata:metadata];
[mySet addObject:vitaminC];
}
if (vitaminDValue > 0){
HKQuantitySample* vitaminD = [HKQuantitySample quantitySampleWithType:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryVitaminD]
quantity:[HKQuantity quantityWithUnit:[HKUnit gramUnit] doubleValue:vitaminDValue]
startDate:timeFoodWasConsumed
endDate:timeFoodWasConsumed
metadata:metadata];
[mySet addObject:vitaminD];
}
if (vitaminEValue > 0){
HKQuantitySample* vitaminE = [HKQuantitySample quantitySampleWithType:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryVitaminE]
quantity:[HKQuantity quantityWithUnit:[HKUnit gramUnit] doubleValue:vitaminEValue]
startDate:timeFoodWasConsumed
endDate:timeFoodWasConsumed
metadata:metadata];
[mySet addObject:vitaminE];
}
if (vitaminKValue > 0){
HKQuantitySample* vitaminK = [HKQuantitySample quantitySampleWithType:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryVitaminK]
quantity:[HKQuantity quantityWithUnit:[HKUnit gramUnit] doubleValue:vitaminKValue]
startDate:timeFoodWasConsumed
endDate:timeFoodWasConsumed
metadata:metadata];
[mySet addObject:vitaminK];
}
if (zincValue > 0){
HKQuantitySample* zinc = [HKQuantitySample quantitySampleWithType:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryZinc]
quantity:[HKQuantity quantityWithUnit:[HKUnit gramUnit] doubleValue:zincValue]
startDate:timeFoodWasConsumed
endDate:timeFoodWasConsumed
metadata:metadata];
[mySet addObject:zinc];
}
// Combine nutritional data into a food correlation //
HKCorrelation* food = [HKCorrelation correlationWithType:[HKCorrelationType correlationTypeForIdentifier:HKCorrelationTypeIdentifierFood]
startDate:timeFoodWasConsumed
endDate:timeFoodWasConsumed
objects:mySet
metadata:metadata];
// Save the food correlation to HealthKit //
[self.healthStore saveObject:food withCompletion:^(BOOL success, NSError *error) {
if (!success) {
NSLog(@"An error occured saving the food sample %@. The error was: ", error);
callback(@[RCTMakeError(@"An error occured saving the food sample", error, nil)]);
return;
}
callback(@[[NSNull null], @true]);
}];
}
- (void)saveWater:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback
{
NSDate *timeWaterWasConsumed = [RCTAppleHealthKit dateFromOptions:input key:@"date" withDefault:[NSDate date]];
double waterValue = [RCTAppleHealthKit doubleFromOptions:input key:@"water" withDefault:(double)0];
HKQuantitySample* water = [HKQuantitySample quantitySampleWithType:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryWater]
quantity:[HKQuantity quantityWithUnit:[HKUnit literUnit] doubleValue:waterValue]
startDate:timeWaterWasConsumed
endDate:timeWaterWasConsumed
metadata:nil];
// Save the water Sample to HealthKit //
[self.healthStore saveObject:water withCompletion:^(BOOL success, NSError *error) {
if (!success) {
NSLog(@"An error occured saving the water sample %@. The error was: ", error);
callback(@[RCTMakeError(@"An error occured saving the water sample", error, nil)]);
return;
}
callback(@[[NSNull null], @true]);
}];
}
@end
......@@ -3,7 +3,8 @@
// RCTAppleHealthKit
//
// Created by Greg Wilson on 2016-06-26.
// Copyright © 2016 Greg Wilson. All rights reserved.
// This source code is licensed under the MIT-style license found in the
// LICENSE file in the root directory of this source tree.
//
#import "RCTAppleHealthKit.h"
......@@ -13,11 +14,17 @@
- (void)fitness_getStepCountOnDay:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)fitness_geStepSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)fitness_geStepSamplesByAnchor:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)fitness_getSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)fitness_setObserver:(NSDictionary *)input;
- (void)fitness_getDailyStepSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)fitness_saveSteps:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)fitness_initializeStepEventObserver:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)fitness_getDistanceWalkingRunningOnDay:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)fitness_getDailyDistanceWalkingRunningSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)fitness_getDailyDistanceSwimmingSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)fitness_getDistanceCyclingOnDay:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)fitness_getDailyDistanceCyclingSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)fitness_getFlightsClimbedOnDay:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)fitness_getDailyFlightsClimbedSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
@end
......@@ -3,7 +3,8 @@
// RCTAppleHealthKit
//
// Created by Greg Wilson on 2016-06-26.
// Copyright © 2016 Greg Wilson. All rights reserved.
// This source code is licensed under the MIT-style license found in the
// LICENSE file in the root directory of this source tree.
//
#import "RCTAppleHealthKit+Methods_Fitness.h"
......@@ -33,8 +34,7 @@
day:date
completion:^(double value, NSDate *startDate, NSDate *endDate, NSError *error) {
if (!value) {
NSLog(@"could not fetch step count for day: %@", error);
callback(@[RCTMakeError(@"could not fetch step count for day", error, nil)]);
callback(@[RCTJSErrorFromNSError(error)]);
return;
}
......@@ -48,6 +48,52 @@
}];
}
- (void)fitness_getSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback
{
HKUnit *unit = [RCTAppleHealthKit hkUnitFromOptions:input key:@"unit" withDefault:[HKUnit countUnit]];
NSUInteger limit = [RCTAppleHealthKit uintFromOptions:input key:@"limit" withDefault:HKObjectQueryNoLimit];
BOOL ascending = [RCTAppleHealthKit boolFromOptions:input key:@"ascending" withDefault:false];
NSString *type = [RCTAppleHealthKit stringFromOptions:input key:@"type" withDefault:@"Walking"];
NSDate *startDate = [RCTAppleHealthKit dateFromOptions:input key:@"startDate" withDefault:[NSDate date]];
NSDate *endDate = [RCTAppleHealthKit dateFromOptions:input key:@"endDate" withDefault:[NSDate date]];
NSPredicate *predicate = [HKQuery predicateForSamplesWithStartDate:startDate endDate:endDate options:HKQueryOptionStrictStartDate];
HKSampleType *samplesType = [RCTAppleHealthKit hkQuantityTypeFromString:type];
if ([type isEqual:@"Running"] || [type isEqual:@"Cycling"]) {
unit = [HKUnit mileUnit];
}
NSLog(@"error getting samples: %@", [samplesType identifier]);
[self fetchSamplesOfType:samplesType
unit:unit
predicate:predicate
ascending:ascending
limit:limit
completion:^(NSArray *results, NSError *error) {
if(results){
callback(@[[NSNull null], results]);
return;
} else {
NSLog(@"error getting samples: %@", error);
callback(@[RCTMakeError(@"error getting samples", nil, nil)]);
return;
}
}];
}
- (void)fitness_setObserver:(NSDictionary *)input
{
HKUnit *unit = [RCTAppleHealthKit hkUnitFromOptions:input key:@"unit" withDefault:[HKUnit countUnit]];
NSString *type = [RCTAppleHealthKit stringFromOptions:input key:@"type" withDefault:@"Walking"];
HKSampleType *samplesType = [RCTAppleHealthKit hkQuantityTypeFromString:type];
if ([type isEqual:@"Running"] || [type isEqual:@"Cycling"]) {
unit = [HKUnit mileUnit];
}
[self setObserverForType:samplesType unit:unit];
}
- (void)fitness_geStepSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback
{
......@@ -115,6 +161,9 @@
NSDate *startDate = [RCTAppleHealthKit dateFromOptions:input key:@"startDate" withDefault:nil];
NSDate *endDate = [RCTAppleHealthKit dateFromOptions:input key:@"endDate" withDefault:[NSDate date]];
NSString *gap = [input objectForKey: @"gap"];
NSUInteger period = [RCTAppleHealthKit uintFromOptions:input key:@"period" withDefault:60];
BOOL includeManuallyAdded = [RCTAppleHealthKit boolFromOptions:input key:@"includeManuallyAdded" withDefault:false];
if(startDate == nil){
callback(@[RCTMakeError(@"startDate is required in options", nil, nil)]);
return;
......@@ -124,15 +173,16 @@
[self fetchCumulativeSumStatisticsCollection:stepCountType
unit:unit
period:period
startDate:startDate
endDate:endDate
ascending:ascending
limit:limit
gap:gap
includeManuallyAdded:includeManuallyAdded
completion:^(NSArray *arr, NSError *err){
if (err != nil) {
NSLog(@"error with fetchCumulativeSumStatisticsCollection: %@", err);
callback(@[RCTMakeError(@"error with fetchCumulativeSumStatisticsCollection", err, nil)]);
callback(@[RCTJSErrorFromNSError(err)]);
return;
}
callback(@[[NSNull null], arr]);
......@@ -158,8 +208,7 @@
[self.healthStore saveObject:sample withCompletion:^(BOOL success, NSError *error) {
if (!success) {
NSLog(@"An error occured saving the step count sample %@. The error was: %@.", sample, error);
callback(@[RCTMakeError(@"An error occured saving the step count sample", error, nil)]);
callback(@[RCTJSErrorFromNSError(error)]);
return;
}
callback(@[[NSNull null], @(value)]);
......@@ -181,9 +230,7 @@
NSError *error) {
if (error) {
// Perform Proper Error Handling Here...
NSLog(@"*** An error occured while setting up the stepCount observer. %@ ***", error.localizedDescription);
callback(@[RCTMakeError(@"An error occured while setting up the stepCount observer", error, nil)]);
callback(@[RCTJSErrorFromNSError(error)]);
return;
}
......@@ -208,8 +255,7 @@
[self fetchSumOfSamplesOnDayForType:quantityType unit:unit day:date completion:^(double distance, NSDate *startDate, NSDate *endDate, NSError *error) {
if (!distance) {
NSLog(@"ERROR getting DistanceWalkingRunning: %@", error);
callback(@[RCTMakeError(@"ERROR getting DistanceWalkingRunning", error, nil)]);
callback(@[RCTJSErrorFromNSError(error)]);
return;
}
......@@ -224,6 +270,72 @@
}];
}
- (void)fitness_getDailyDistanceWalkingRunningSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback
{
HKUnit *unit = [RCTAppleHealthKit hkUnitFromOptions:input key:@"unit" withDefault:[HKUnit meterUnit]];
NSUInteger limit = [RCTAppleHealthKit uintFromOptions:input key:@"limit" withDefault:HKObjectQueryNoLimit];
BOOL ascending = [RCTAppleHealthKit boolFromOptions:input key:@"ascending" withDefault:false];
NSDate *startDate = [RCTAppleHealthKit dateFromOptions:input key:@"startDate" withDefault:nil];
NSDate *endDate = [RCTAppleHealthKit dateFromOptions:input key:@"endDate" withDefault:[NSDate date]];
NSUInteger period = [RCTAppleHealthKit uintFromOptions:input key:@"period" withDefault:60];
BOOL includeManuallyAdded = [RCTAppleHealthKit boolFromOptions:input key:@"includeManuallyAdded" withDefault:false];
if(startDate == nil){
callback(@[RCTMakeError(@"startDate is required in options", nil, nil)]);
return;
}
HKQuantityType *quantityType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDistanceWalkingRunning];
[self fetchCumulativeSumStatisticsCollection:quantityType
unit:unit
period:period
startDate:startDate
endDate:endDate
ascending:ascending
limit:limit
includeManuallyAdded:includeManuallyAdded
completion:^(NSArray *arr, NSError *err){
if (err != nil) {
NSLog(@"error with fetchCumulativeSumStatisticsCollection: %@", err);
callback(@[RCTMakeError(@"error with fetchCumulativeSumStatisticsCollection", err, nil)]);
return;
}
callback(@[[NSNull null], arr]);
}];
}
- (void)fitness_getDailyDistanceSwimmingSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback
{
HKUnit *unit = [RCTAppleHealthKit hkUnitFromOptions:input key:@"unit" withDefault:[HKUnit meterUnit]];
NSUInteger limit = [RCTAppleHealthKit uintFromOptions:input key:@"limit" withDefault:HKObjectQueryNoLimit];
BOOL ascending = [RCTAppleHealthKit boolFromOptions:input key:@"ascending" withDefault:false];
NSDate *startDate = [RCTAppleHealthKit dateFromOptions:input key:@"startDate" withDefault:nil];
NSDate *endDate = [RCTAppleHealthKit dateFromOptions:input key:@"endDate" withDefault:[NSDate date]];
NSUInteger period = [RCTAppleHealthKit uintFromOptions:input key:@"period" withDefault:60];
BOOL includeManuallyAdded = [RCTAppleHealthKit boolFromOptions:input key:@"includeManuallyAdded" withDefault:false];
if(startDate == nil){
callback(@[RCTMakeError(@"startDate is required in options", nil, nil)]);
return;
}
HKQuantityType *quantityType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDistanceSwimming];
[self fetchCumulativeSumStatisticsCollection:quantityType
unit:unit
period:period
startDate:startDate
endDate:endDate
ascending:ascending
limit:limit
includeManuallyAdded:includeManuallyAdded
completion:^(NSArray *arr, NSError *err){
if (err != nil) {
callback(@[RCTJSErrorFromNSError(err)]);
return;
}
callback(@[[NSNull null], arr]);
}];
}
- (void)fitness_getDistanceCyclingOnDay:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback
{
......@@ -234,8 +346,7 @@
[self fetchSumOfSamplesOnDayForType:quantityType unit:unit day:date completion:^(double distance, NSDate *startDate, NSDate *endDate, NSError *error) {
if (!distance) {
NSLog(@"ERROR getting DistanceCycling: %@", error);
callback(@[RCTMakeError(@"ERROR getting DistanceCycling", error, nil)]);
callback(@[RCTJSErrorFromNSError(error)]);
return;
}
......@@ -249,6 +360,38 @@
}];
}
- (void)fitness_getDailyDistanceCyclingSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback
{
HKUnit *unit = [RCTAppleHealthKit hkUnitFromOptions:input key:@"unit" withDefault:[HKUnit meterUnit]];
NSUInteger limit = [RCTAppleHealthKit uintFromOptions:input key:@"limit" withDefault:HKObjectQueryNoLimit];
BOOL ascending = [RCTAppleHealthKit boolFromOptions:input key:@"ascending" withDefault:false];
NSDate *startDate = [RCTAppleHealthKit dateFromOptions:input key:@"startDate" withDefault:nil];
NSDate *endDate = [RCTAppleHealthKit dateFromOptions:input key:@"endDate" withDefault:[NSDate date]];
NSUInteger period = [RCTAppleHealthKit uintFromOptions:input key:@"period" withDefault:60];
BOOL includeManuallyAdded = [RCTAppleHealthKit boolFromOptions:input key:@"includeManuallyAdded" withDefault:false];
if(startDate == nil){
callback(@[RCTMakeError(@"startDate is required in options", nil, nil)]);
return;
}
HKQuantityType *quantityType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDistanceCycling];
[self fetchCumulativeSumStatisticsCollection:quantityType
unit:unit
period:period
startDate:startDate
endDate:endDate
ascending:ascending
limit:limit
includeManuallyAdded:includeManuallyAdded
completion:^(NSArray *arr, NSError *err){
if (err != nil) {
callback(@[RCTJSErrorFromNSError(err)]);
return;
}
callback(@[[NSNull null], arr]);
}];
}
- (void)fitness_getFlightsClimbedOnDay:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback
{
......@@ -259,8 +402,7 @@
[self fetchSumOfSamplesOnDayForType:quantityType unit:unit day:date completion:^(double count, NSDate *startDate, NSDate *endDate, NSError *error) {
if (!count) {
NSLog(@"ERROR getting FlightsClimbed: %@", error);
callback(@[RCTMakeError(@"ERROR getting FlightsClimbed", error, nil), @(count)]);
callback(@[RCTJSErrorFromNSError(error)]);
return;
}
......@@ -274,4 +416,33 @@
}];
}
- (void)fitness_getDailyFlightsClimbedSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback
{
HKUnit *unit = [RCTAppleHealthKit hkUnitFromOptions:input key:@"unit" withDefault:[HKUnit countUnit]];
NSUInteger limit = [RCTAppleHealthKit uintFromOptions:input key:@"limit" withDefault:HKObjectQueryNoLimit];
BOOL ascending = [RCTAppleHealthKit boolFromOptions:input key:@"ascending" withDefault:false];
NSDate *startDate = [RCTAppleHealthKit dateFromOptions:input key:@"startDate" withDefault:nil];
NSDate *endDate = [RCTAppleHealthKit dateFromOptions:input key:@"endDate" withDefault:[NSDate date]];
if(startDate == nil){
callback(@[RCTMakeError(@"startDate is required in options", nil, nil)]);
return;
}
HKQuantityType *quantityType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierFlightsClimbed];
[self fetchCumulativeSumStatisticsCollection:quantityType
unit:unit
startDate:startDate
endDate:endDate
ascending:ascending
limit:limit
completion:^(NSArray *arr, NSError *err){
if (err != nil) {
callback(@[RCTJSErrorFromNSError(err)]);
return;
}
callback(@[[NSNull null], arr]);
}];
}
@end
......@@ -29,8 +29,7 @@
[self.healthStore saveObject:sample withCompletion:^(BOOL success, NSError *error) {
if (!success) {
NSLog(@"An error occured saving the mindful session sample %@. The error was: %@.", sample, error);
callback(@[RCTMakeError(@"An error occured saving the mindful session sample", error, nil)]);
callback(@[RCTJSErrorFromNSError(error)]);
return;
}
callback(@[[NSNull null], @(value)]);
......
......@@ -32,8 +32,7 @@
callback(@[[NSNull null], results]);
return;
} else {
NSLog(@"error getting blood glucose samples: %@", error);
callback(@[RCTMakeError(@"error getting blood glucose samples", nil, nil)]);
callback(@[RCTJSErrorFromNSError(error)]);
return;
}
}];
......
......@@ -3,7 +3,8 @@
// RCTAppleHealthKit
//
// Created by Greg Wilson on 2016-11-06.
// Copyright © 2016 Greg Wilson. All rights reserved.
// This source code is licensed under the MIT-style license found in the
// LICENSE file in the root directory of this source tree.
//
#import "RCTAppleHealthKit.h"
......
......@@ -3,7 +3,8 @@
// RCTAppleHealthKit
//
// Created by Greg Wilson on 2016-11-06.
// Copyright © 2016 Greg Wilson. All rights reserved.
// This source code is licensed under the MIT-style license found in the
// LICENSE file in the root directory of this source tree.
//
......@@ -35,8 +36,7 @@
callback(@[[NSNull null], results]);
return;
} else {
NSLog(@"error getting sleep samples: %@", error);
callback(@[RCTMakeError(@"error getting sleep samples", nil, nil)]);
callback(@[RCTJSErrorFromNSError(error)]);
return;
}
}];
......
......@@ -33,8 +33,7 @@
callback(@[[NSNull null], results]);
return;
} else {
NSLog(@"error getting heart rate samples: %@", error);
callback(@[RCTMakeError(@"error getting heart rate samples", nil, nil)]);
callback(@[RCTJSErrorFromNSError(error)]);
return;
}
}];
......
......@@ -3,7 +3,8 @@
// RCTAppleHealthKit
//
// Created by Greg Wilson on 2016-06-26.
// Copyright © 2016 Greg Wilson. All rights reserved.
// This source code is licensed under the MIT-style license found in the
// LICENSE file in the root directory of this source tree.
//
#import "RCTAppleHealthKit.h"
......@@ -18,6 +19,18 @@
startDate:(NSDate *)startDate
endDate:(NSDate *)endDate
completion:(void (^)(NSArray *, NSError *))completionHandler;
- (void)fetchSamplesOfType:(HKSampleType *)quantityType
unit:(HKUnit *)unit
predicate:(NSPredicate *)predicate
ascending:(BOOL)asc
limit:(NSUInteger)lim
completion:(void (^)(NSArray *, NSError *))completion;
- (void)setObserverForType:(HKSampleType *)quantityType
unit:(HKUnit *)unit;
- (void)fetchQuantitySamplesOfType:(HKQuantityType *)quantityType
unit:(HKUnit *)unit
predicate:(NSPredicate *)predicate
......@@ -43,8 +56,24 @@
ascending:(BOOL)asc
limit:(NSUInteger)lim
gap:(NSString *)gap
includeManuallyAdded:(BOOL)includeManuallyAdded
completion:(void (^)(NSArray *, NSError *))completionHandler;
- (void)fetchCumulativeSumStatisticsCollection:(HKQuantityType *)quantityType
unit:(HKUnit *)unit
startDate:(NSDate *)startDate
endDate:(NSDate *)endDate
ascending:(BOOL)asc
limit:(NSUInteger)lim
completion:(void (^)(NSArray *, NSError *))completionHandler;
- (void)fetchCumulativeSumStatisticsCollection:(HKQuantityType *)quantityType
unit:(HKUnit *)unit
period:(NSUInteger)period
startDate:(NSDate *)startDate
endDate:(NSDate *)endDate
ascending:(BOOL)asc
limit:(NSUInteger)lim
includeManuallyAdded:(BOOL)includeManuallyAdded
completion:(void (^)(NSArray *, NSError *))completionHandler;
- (void)fetchSleepCategorySamplesForPredicate:(NSPredicate *)predicate
......
......@@ -3,14 +3,17 @@
// RCTAppleHealthKit
//
// Created by Greg Wilson on 2016-06-26.
// Copyright © 2016 Greg Wilson. All rights reserved.
// This source code is licensed under the MIT-style license found in the
// LICENSE file in the root directory of this source tree.
//
#import "RCTAppleHealthKit+Queries.h"
#import "RCTAppleHealthKit+Utils.h"
@implementation RCTAppleHealthKit (Queries)
#import <React/RCTBridgeModule.h>
#import <React/RCTEventDispatcher.h>
@implementation RCTAppleHealthKit (Queries)
- (void)fetchMostRecentQuantitySampleOfType:(HKQuantityType *)quantityType
predicate:(NSPredicate *)predicate
......@@ -48,7 +51,6 @@
[self.healthStore executeQuery:query];
}
- (void)fetchQuantitySamplesOfType:(HKQuantityType *)quantityType
unit:(HKUnit *)unit
predicate:(NSPredicate *)predicate
......@@ -128,7 +130,6 @@
[self.healthStore executeQuery:query];
}
- (void)fetchQuantityDoubleSamplesOfType:(HKQuantityType *)quantityType
unit:(HKUnit *)unit
predicate:(NSPredicate *)predicate
......@@ -208,17 +209,154 @@
[self.healthStore executeQuery:query];
}
- (void)fetchSamplesOfType:(HKSampleType *)type
unit:(HKUnit *)unit
predicate:(NSPredicate *)predicate
ascending:(BOOL)asc
limit:(NSUInteger)lim
completion:(void (^)(NSArray *, NSError *))completion {
NSSortDescriptor *timeSortDescriptor = [[NSSortDescriptor alloc] initWithKey:HKSampleSortIdentifierEndDate
ascending:asc];
// declare the block
void (^handlerBlock)(HKSampleQuery *query, NSArray *results, NSError *error);
// create and assign the block
handlerBlock = ^(HKSampleQuery *query, NSArray *results, NSError *error) {
if (!results) {
if (completion) {
completion(nil, error);
}
return;
}
if (completion) {
NSMutableArray *data = [NSMutableArray arrayWithCapacity:1];
dispatch_async(dispatch_get_main_queue(), ^{
if (type == [HKObjectType workoutType]) {
for (HKWorkout *sample in results) {
double energy = [[sample totalEnergyBurned] doubleValueForUnit:[HKUnit kilocalorieUnit]];
double distance = [[sample totalDistance] doubleValueForUnit:[HKUnit mileUnit]];
NSString *type = [RCTAppleHealthKit stringForHKWorkoutActivityType:[sample workoutActivityType]];
NSString *startDateString = [RCTAppleHealthKit buildISO8601StringFromDate:sample.startDate];
NSString *endDateString = [RCTAppleHealthKit buildISO8601StringFromDate:sample.endDate];
bool isTracked = true;
if ([[sample metadata][HKMetadataKeyWasUserEntered] intValue] == 1) {
isTracked = false;
}
NSString* device = @"";
if (@available(iOS 11.0, *)) {
device = [[sample sourceRevision] productType];
} else {
device = [[sample device] name];
if (!device) {
device = @"iPhone";
}
}
NSDictionary *elem = @{
@"activityId" : [NSNumber numberWithInt:[sample workoutActivityType]],
@"activityName" : type,
@"calories" : @(energy),
@"tracked" : @(isTracked),
@"sourceName" : [[[sample sourceRevision] source] name],
@"sourceId" : [[[sample sourceRevision] source] bundleIdentifier],
@"device": device,
@"distance" : @(distance),
@"start" : startDateString,
@"end" : endDateString
};
[data addObject:elem];
}
} else {
for (HKQuantitySample *sample in results) {
HKQuantity *quantity = sample.quantity;
double value = [quantity doubleValueForUnit:unit];
NSString * valueType = @"quantity";
if (unit == [HKUnit mileUnit]) {
valueType = @"distance";
}
NSString *startDateString = [RCTAppleHealthKit buildISO8601StringFromDate:sample.startDate];
NSString *endDateString = [RCTAppleHealthKit buildISO8601StringFromDate:sample.endDate];
bool isTracked = true;
if ([[sample metadata][HKMetadataKeyWasUserEntered] intValue] == 1) {
isTracked = false;
}
NSString* device = @"";
if (@available(iOS 11.0, *)) {
device = [[sample sourceRevision] productType];
} else {
device = [[sample device] name];
if (!device) {
device = @"iPhone";
}
}
NSDictionary *elem = @{
valueType : @(value),
@"tracked" : @(isTracked),
@"sourceName" : [[[sample sourceRevision] source] name],
@"sourceId" : [[[sample sourceRevision] source] bundleIdentifier],
@"device": device,
@"start" : startDateString,
@"end" : endDateString
};
[data addObject:elem];
}
}
completion(data, error);
});
}
};
HKSampleQuery *query = [[HKSampleQuery alloc] initWithSampleType:type
predicate:predicate
limit:lim
sortDescriptors:@[timeSortDescriptor]
resultsHandler:handlerBlock];
[self.healthStore executeQuery:query];
}
- (void)setObserverForType:(HKSampleType *)type
unit:(HKUnit *)unit {
HKObserverQuery *query = [[HKObserverQuery alloc] initWithSampleType:type predicate:nil updateHandler:^(HKObserverQuery *query, HKObserverQueryCompletionHandler completionHandler, NSError * _Nullable error){
if (error) {
NSLog(@"*** An error occured while setting up the stepCount observer. %@ ***", error.localizedDescription);
return;
}
[self.bridge.eventDispatcher sendAppEventWithName:@"observer" body:@""];
// Theoretically, HealthKit expect that copletionHandler would be called at the end of query process,
// but it's unclear how to do in in event paradigm
// dispatch_time_t delay = dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC * 5);
// dispatch_after(delay, dispatch_get_main_queue(), ^(void){
// completionHandler();
// });
}];
[self.healthStore executeQuery:query];
[self.healthStore enableBackgroundDeliveryForType:type frequency:HKUpdateFrequencyImmediate withCompletion:^(BOOL success, NSError * _Nullable error) {
NSLog(@"success %s print some error %@", success ? "true" : "false", [error localizedDescription]);
}];
}
- (void)fetchSleepCategorySamplesForPredicate:(NSPredicate *)predicate
limit:(NSUInteger)lim
completion:(void (^)(NSArray *, NSError *))completion {
NSSortDescriptor *timeSortDescriptor = [[NSSortDescriptor alloc] initWithKey:HKSampleSortIdentifierEndDate
ascending:true];
......@@ -409,10 +547,10 @@
fromDate:[NSDate date]];
anchorComponents.hour = 0;
NSDate *anchorDate = [calendar dateFromComponents:anchorComponents];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"metadata.%K != YES", HKMetadataKeyWasUserEntered];
// Create the query
HKStatisticsCollectionQuery *query = [[HKStatisticsCollectionQuery alloc] initWithQuantityType:quantityType
quantitySamplePredicate:nil
quantitySamplePredicate:predicate
options:HKStatisticsOptionCumulativeSum
anchorDate:anchorDate
intervalComponents:interval];
......@@ -455,6 +593,7 @@
ascending:(BOOL)asc
limit:(NSUInteger)lim
gap:(NSString *)gap
includeManuallyAdded:(BOOL)includeManuallyAdded
completion:(void (^)(NSArray *, NSError *))completionHandler {
NSCalendar *calendar = [NSCalendar currentCalendar];
......@@ -469,10 +608,159 @@
fromDate:[NSDate date]];
anchorComponents.hour = 0;
NSDate *anchorDate = [calendar dateFromComponents:anchorComponents];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"metadata.%K != YES", HKMetadataKeyWasUserEntered];
// Create the query
HKStatisticsCollectionQuery *query = [[HKStatisticsCollectionQuery alloc] initWithQuantityType:quantityType
quantitySamplePredicate:predicate
options:HKStatisticsOptionCumulativeSum
anchorDate:anchorDate
intervalComponents:interval];
// Set the results handler
query.initialResultsHandler = ^(HKStatisticsCollectionQuery *query, HKStatisticsCollection *results, NSError *error) {
if (error) {
// Perform proper error handling here
NSLog(@"*** An error occurred while calculating the statistics: %@ ***", error.localizedDescription);
}
NSMutableArray *data = [NSMutableArray arrayWithCapacity:1];
[results enumerateStatisticsFromDate:startDate
toDate:endDate
withBlock:^(HKStatistics *result, BOOL *stop) {
HKQuantity *quantity = result.sumQuantity;
if (quantity) {
NSDate *startDate = result.startDate;
NSDate *endDate = result.endDate;
double value = [quantity doubleValueForUnit:unit];
NSString *startDateString = [RCTAppleHealthKit buildISO8601StringFromDate:startDate];
NSString *endDateString = [RCTAppleHealthKit buildISO8601StringFromDate:endDate];
NSDictionary *elem = @{
@"value" : @(value),
@"startDate" : startDateString,
@"endDate" : endDateString,
};
[data addObject:elem];
}
}];
// is ascending by default
if(asc == false) {
[RCTAppleHealthKit reverseNSMutableArray:data];
}
if((lim > 0) && ([data count] > lim)) {
NSArray* slicedArray = [data subarrayWithRange:NSMakeRange(0, lim)];
NSError *err;
completionHandler(slicedArray, err);
} else {
NSError *err;
completionHandler(data, err);
}
};
[self.healthStore executeQuery:query];
}
- (void)fetchCumulativeSumStatisticsCollection:(HKQuantityType *)quantityType
unit:(HKUnit *)unit
startDate:(NSDate *)startDate
endDate:(NSDate *)endDate
ascending:(BOOL)asc
limit:(NSUInteger)lim
completion:(void (^)(NSArray *, NSError *))completionHandler {
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *interval = [[NSDateComponents alloc] init];
interval.day = 1;
NSDateComponents *anchorComponents = [calendar components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear
fromDate:[NSDate date]];
anchorComponents.hour = 0;
NSDate *anchorDate = [calendar dateFromComponents:anchorComponents];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"metadata.%K != YES", HKMetadataKeyWasUserEntered];
// Create the query
HKStatisticsCollectionQuery *query = [[HKStatisticsCollectionQuery alloc] initWithQuantityType:quantityType
quantitySamplePredicate:predicate
options:HKStatisticsOptionCumulativeSum
anchorDate:anchorDate
intervalComponents:interval];
// Set the results handler
query.initialResultsHandler = ^(HKStatisticsCollectionQuery *query, HKStatisticsCollection *results, NSError *error) {
if (error) {
// Perform proper error handling here
NSLog(@"*** An error occurred while calculating the statistics: %@ ***", error.localizedDescription);
}
NSMutableArray *data = [NSMutableArray arrayWithCapacity:1];
[results enumerateStatisticsFromDate:startDate
toDate:endDate
withBlock:^(HKStatistics *result, BOOL *stop) {
HKQuantity *quantity = result.sumQuantity;
if (quantity) {
NSDate *startDate = result.startDate;
NSDate *endDate = result.endDate;
double value = [quantity doubleValueForUnit:unit];
NSString *startDateString = [RCTAppleHealthKit buildISO8601StringFromDate:startDate];
NSString *endDateString = [RCTAppleHealthKit buildISO8601StringFromDate:endDate];
NSDictionary *elem = @{
@"value" : @(value),
@"startDate" : startDateString,
@"endDate" : endDateString,
};
[data addObject:elem];
}
}];
// is ascending by default
if(asc == false) {
[RCTAppleHealthKit reverseNSMutableArray:data];
}
if((lim > 0) && ([data count] > lim)) {
NSArray* slicedArray = [data subarrayWithRange:NSMakeRange(0, lim)];
NSError *err;
completionHandler(slicedArray, err);
} else {
NSError *err;
completionHandler(data, err);
}
};
[self.healthStore executeQuery:query];
}
- (void)fetchCumulativeSumStatisticsCollection:(HKQuantityType *)quantityType
unit:(HKUnit *)unit
period:(NSUInteger)period
startDate:(NSDate *)startDate
endDate:(NSDate *)endDate
ascending:(BOOL)asc
limit:(NSUInteger)lim
includeManuallyAdded:(BOOL)includeManuallyAdded
completion:(void (^)(NSArray *, NSError *))completionHandler {
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *interval = [[NSDateComponents alloc] init];
interval.minute = period;
NSDateComponents *anchorComponents = [calendar components:NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond | NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear
fromDate:startDate];
//anchorComponents.hour = 0;
NSDate *anchorDate = [calendar dateFromComponents:anchorComponents];
NSPredicate *predicate = nil;
if (includeManuallyAdded == false) {
predicate = [NSPredicate predicateWithFormat:@"metadata.%K != YES", HKMetadataKeyWasUserEntered];
}
// Create the query
HKStatisticsCollectionQuery *query = [[HKStatisticsCollectionQuery alloc] initWithQuantityType:quantityType
quantitySamplePredicate:nil
quantitySamplePredicate:predicate
options:HKStatisticsOptionCumulativeSum
anchorDate:anchorDate
intervalComponents:interval];
......
......@@ -3,14 +3,19 @@
// RCTAppleHealthKit
//
// Created by Greg Wilson on 2016-06-26.
// Copyright © 2016 Greg Wilson. All rights reserved.
// This source code is licensed under the MIT-style license found in the
// LICENSE file in the root directory of this source tree.
//
#import "RCTAppleHealthKit.h"
@interface RCTAppleHealthKit (TypesAndPermissions)
- (NSDictionary *)readPermsDict;
- (NSDictionary *)writePermsDict;
- (NSSet *)getReadPermsFromOptions:(NSArray *)options;
- (NSSet *)getWritePermsFromOptions:(NSArray *)options;
- (HKObjectType *)getWritePermFromString:(NSString *)string;
- (NSString *)getAuthorizationStatusString:(HKAuthorizationStatus)status;
@end
......@@ -3,7 +3,8 @@
// RCTAppleHealthKit
//
// Created by Greg Wilson on 2016-06-26.
// Copyright © 2016 Greg Wilson. All rights reserved.
// This source code is licensed under the MIT-style license found in the
// LICENSE file in the root directory of this source tree.
//
#import "RCTAppleHealthKit+TypesAndPermissions.h"
......@@ -30,6 +31,7 @@
@"StepCount" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount],
@"DistanceWalkingRunning" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDistanceWalkingRunning],
@"DistanceCycling" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDistanceCycling],
@"DistanceSwimming" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDistanceSwimming],
@"BasalEnergyBurned" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierBasalEnergyBurned],
@"ActiveEnergyBurned" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierActiveEnergyBurned],
@"FlightsClimbed" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierFlightsClimbed],
......@@ -49,6 +51,8 @@
@"SleepAnalysis" : [HKObjectType categoryTypeForIdentifier:HKCategoryTypeIdentifierSleepAnalysis],
// Mindfulness
@"MindfulSession" : [HKObjectType categoryTypeForIdentifier:HKCategoryTypeIdentifierMindfulSession],
//workouts
@"Workout" : [HKObjectType workoutType],
};
return readPerms;
}
......@@ -72,7 +76,44 @@
@"ActiveEnergyBurned" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierActiveEnergyBurned],
@"FlightsClimbed" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierFlightsClimbed],
// Nutrition Identifiers
@"DietaryEnergy" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryEnergyConsumed],
@"Biotin" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryBiotin],
@"Caffeine" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryCaffeine],
@"Calcium" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryCalcium],
@"Carbohydrates" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryCarbohydrates],
@"Chloride" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryChloride],
@"Cholesterol" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryCholesterol],
@"Copper" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryCopper],
@"EnergyConsumed" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryEnergyConsumed],
@"FatMonounsaturated" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryFatMonounsaturated],
@"FatPolyunsaturated" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryFatPolyunsaturated],
@"FatSaturated" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryFatSaturated],
@"FatTotal" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryFatTotal],
@"Fiber" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryFiber],
@"Folate" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryFolate],
@"Iodine" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryIodine],
@"Iron" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryIron],
@"Magnesium" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryMagnesium],
@"Manganese" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryManganese],
@"Molybdenum" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryMolybdenum],
@"Niacin" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryNiacin],
@"PantothenicAcid" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryPantothenicAcid],
@"Phosphorus" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryPhosphorus],
@"Potassium" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryPotassium],
@"Protein" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryProtein],
@"Riboflavin" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryRiboflavin],
@"Selenium" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietarySelenium],
@"Sodium" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietarySodium],
@"Sugar" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietarySugar],
@"Thiamin" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryThiamin],
@"VitaminA" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryVitaminA],
@"VitaminB12" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryVitaminB12],
@"VitaminB6" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryVitaminB6],
@"VitaminC" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryVitaminC],
@"VitaminD" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryVitaminD],
@"VitaminE" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryVitaminE],
@"VitaminK" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryVitaminK],
@"Zinc" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryZinc],
@"Water" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryWater],
// Sleep
@"SleepAnalysis" : [HKObjectType categoryTypeForIdentifier:HKCategoryTypeIdentifierSleepAnalysis],
// Mindfulness
......@@ -81,7 +122,6 @@
return writePerms;
}
// Returns HealthKit read permissions from options array
- (NSSet *)getReadPermsFromOptions:(NSArray *)options {
NSDictionary *readPermDict = [self readPermsDict];
......@@ -113,4 +153,18 @@
return writePermSet;
}
- (HKObjectType *)getWritePermFromString:(NSString *)writePerm {
return [[self writePermsDict] objectForKey:writePerm];
}
- (NSString *)getAuthorizationStatusString:(HKAuthorizationStatus)status {
switch (status) {
case HKAuthorizationStatusNotDetermined:
return @"NotDetermined";
case HKAuthorizationStatusSharingDenied:
return @"SharingDenied";
case HKAuthorizationStatusSharingAuthorized:
return @"SharingAuthorized";
}
}
@end
......@@ -3,7 +3,8 @@
// RCTAppleHealthKit
//
// Created by Greg Wilson on 2016-06-26.
// Copyright © 2016 Greg Wilson. All rights reserved.
// This source code is licensed under the MIT-style license found in the
// LICENSE file in the root directory of this source tree.
//
#import "RCTAppleHealthKit.h"
......@@ -23,7 +24,7 @@
+ (NSDate *)startDateFromOptions:(NSDictionary *)options;
+ (NSDate *)endDateFromOptions:(NSDictionary *)options;
+ (NSDate *)endDateFromOptionsDefaultNow:(NSDictionary *)options;
+ (HKUnit *)hkUnitFromOptions:(NSDictionary *)options;
+ (HKSampleType *)hkQuantityTypeFromString:(NSString *)type;
+ (HKUnit *)hkUnitFromOptions:(NSDictionary *)options key:(NSString *)key withDefault:(HKUnit *)defaultValue;
+ (NSUInteger)uintFromOptions:(NSDictionary *)options key:(NSString *)key withDefault:(NSUInteger)defaultValue;
......@@ -33,6 +34,7 @@
+ (bool)boolFromOptions:(NSDictionary *)options key:(NSString *)key withDefault:(bool)defaultValue;
+ (NSMutableArray *)reverseNSMutableArray:(NSMutableArray *)array;
+ (NSString*) stringForHKWorkoutActivityType:(int) enumValue;
+ (NSString *)stringFromType:(HKSampleType *)type status:(bool)status;
......
......@@ -3,7 +3,8 @@
// RCTAppleHealthKit
//
// Created by Greg Wilson on 2016-06-26.
// Copyright © 2016 Greg Wilson. All rights reserved.
// This source code is licensed under the MIT-style license found in the
// LICENSE file in the root directory of this source tree.
//
#import "RCTAppleHealthKit+Utils.h"
......@@ -131,56 +132,20 @@
return date;
}
// ==========
// DEPRECATED
// ==========
+ (HKUnit *)hkUnitFromOptions:(NSDictionary *)options {
NSString *unitString = [options objectForKey:@"unit"];
HKUnit *theUnit;
if([unitString isEqualToString:@"gram"]){
theUnit = [HKUnit gramUnit];
}
if([unitString isEqualToString:@"pound"]){
theUnit = [HKUnit poundUnit];
}
if([unitString isEqualToString:@"meter"]){
theUnit = [HKUnit meterUnit];
}
if([unitString isEqualToString:@"mile"]){
theUnit = [HKUnit mileUnit];
}
if([unitString isEqualToString:@"inch"]){
theUnit = [HKUnit inchUnit];
}
if([unitString isEqualToString:@"foot"]){
theUnit = [HKUnit footUnit];
}
if([unitString isEqualToString:@"second"]){
theUnit = [HKUnit secondUnit];
}
if([unitString isEqualToString:@"minute"]){
theUnit = [HKUnit minuteUnit];
}
if([unitString isEqualToString:@"hour"]){
theUnit = [HKUnit hourUnit];
}
if([unitString isEqualToString:@"day"]){
theUnit = [HKUnit dayUnit];
}
if([unitString isEqualToString:@"joule"]){
theUnit = [HKUnit jouleUnit];
}
if([unitString isEqualToString:@"calorie"]){
theUnit = [HKUnit calorieUnit];
}
if([unitString isEqualToString:@"count"]){
theUnit = [HKUnit countUnit];
}
if([unitString isEqualToString:@"percent"]){
theUnit = [HKUnit percentUnit];
}
return theUnit;
+ (HKSampleType *)hkQuantityTypeFromString:(NSString *)type {
if ([type isEqual:@"Walking"]) {
return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];
} else if ([type isEqual:@"StairClimbing"]) {
return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierFlightsClimbed];
} else if ([type isEqual:@"Running"]){
return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDistanceWalkingRunning];
} else if ([type isEqual:@"Cycling"]){
return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDistanceCycling];
} else if ([type isEqual:@"Swimming"]){
return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDistanceSwimming];
}
// default [type isEqual:@"Workout"])
return [HKObjectType workoutType];
}
......@@ -191,12 +156,21 @@
if([unitString isEqualToString:@"gram"]){
theUnit = [HKUnit gramUnit];
}
if([unitString isEqualToString:@"kg"]){
theUnit = [HKUnit gramUnitWithMetricPrefix:HKMetricPrefixKilo];
}
if([unitString isEqualToString:@"stone"]){
theUnit = [HKUnit stoneUnit];
}
if([unitString isEqualToString:@"pound"]){
theUnit = [HKUnit poundUnit];
}
if([unitString isEqualToString:@"meter"]){
theUnit = [HKUnit meterUnit];
}
if([unitString isEqualToString:@"cm"]){
theUnit = [HKUnit meterUnitWithMetricPrefix:HKMetricPrefixCenti];
}
if([unitString isEqualToString:@"inch"]){
theUnit = [HKUnit inchUnit];
}
......@@ -347,5 +321,166 @@
}
return str;
}
+ (NSString*)stringForHKWorkoutActivityType:(int) enumValue{
switch( enumValue ){
case HKWorkoutActivityTypeAmericanFootball:
return @"AmericanFootball";
case HKWorkoutActivityTypeArchery:
return @"Archery";
case HKWorkoutActivityTypeAustralianFootball:
return @"AustralianFootball";
case HKWorkoutActivityTypeBadminton:
return @"Badminton";
case HKWorkoutActivityTypeBaseball:
return @"Baseball";
case HKWorkoutActivityTypeBasketball:
return @"Basketball";
case HKWorkoutActivityTypeBowling:
return @"Bowling";
case HKWorkoutActivityTypeBoxing:
return @"Boxing";
case HKWorkoutActivityTypeClimbing:
return @"Climbing";
case HKWorkoutActivityTypeCricket:
return @"Cricket";
case HKWorkoutActivityTypeCrossTraining:
return @"CrossTraining";
case HKWorkoutActivityTypeCurling:
return @"Curling";
case HKWorkoutActivityTypeCycling:
return @"Cycling";
case HKWorkoutActivityTypeDance:
return @"Dance";
case HKWorkoutActivityTypeDanceInspiredTraining:
return @"DanceInspiredTraining";
case HKWorkoutActivityTypeElliptical:
return @"Elliptical";
case HKWorkoutActivityTypeEquestrianSports:
return @"EquestrianSports";
case HKWorkoutActivityTypeFencing:
return @"Fencing";
case HKWorkoutActivityTypeFishing:
return @"Fishing";
case HKWorkoutActivityTypeFunctionalStrengthTraining:
return @"FunctionalStrengthTraining";
case HKWorkoutActivityTypeGolf:
return @"Golf";
case HKWorkoutActivityTypeGymnastics:
return @"Gymnastics";
case HKWorkoutActivityTypeHandball:
return @"Handball";
case HKWorkoutActivityTypeHiking:
return @"Hiking";
case HKWorkoutActivityTypeHockey:
return @"Hockey";
case HKWorkoutActivityTypeHunting:
return @"Hunting";
case HKWorkoutActivityTypeLacrosse:
return @"Lacrosse";
case HKWorkoutActivityTypeMartialArts:
return @"MartialArts";
case HKWorkoutActivityTypeMindAndBody:
return @"MindAndBody";
case HKWorkoutActivityTypeMixedMetabolicCardioTraining:
return @"MixedMetabolicCardioTraining";
case HKWorkoutActivityTypePaddleSports:
return @"PaddleSports";
case HKWorkoutActivityTypePlay:
return @"Play";
case HKWorkoutActivityTypePreparationAndRecovery:
return @"PreparationAndRecovery";
case HKWorkoutActivityTypeRacquetball:
return @"Racquetball";
case HKWorkoutActivityTypeRowing:
return @"Rowing";
case HKWorkoutActivityTypeRugby:
return @"Rugby";
case HKWorkoutActivityTypeRunning:
return @"Running";
case HKWorkoutActivityTypeSailing:
return @"Sailing";
case HKWorkoutActivityTypeSkatingSports:
return @"SkatingSports";
case HKWorkoutActivityTypeSnowSports:
return @"SnowSports";
case HKWorkoutActivityTypeSoccer:
return @"Soccer";
case HKWorkoutActivityTypeSoftball:
return @"Softball";
case HKWorkoutActivityTypeSquash:
return @"Squash";
case HKWorkoutActivityTypeStairClimbing:
return @"StairClimbing";
case HKWorkoutActivityTypeSurfingSports:
return @"SurfingSports";
case HKWorkoutActivityTypeSwimming:
return @"Swimming";
case HKWorkoutActivityTypeTableTennis:
return @"TableTennis";
case HKWorkoutActivityTypeTennis:
return @"Tennis";
case HKWorkoutActivityTypeTrackAndField:
return @"TrackAndField";
case HKWorkoutActivityTypeTraditionalStrengthTraining:
return @"TraditionalStrengthTraining";
case HKWorkoutActivityTypeVolleyball:
return @"Volleyball";
case HKWorkoutActivityTypeWalking:
return @"Walking";
case HKWorkoutActivityTypeWaterFitness:
return @"WaterFitness";
case HKWorkoutActivityTypeWaterPolo:
return @"WaterPolo";
case HKWorkoutActivityTypeWaterSports:
return @"WaterSports";
case HKWorkoutActivityTypeWrestling:
return @"Wrestling";
case HKWorkoutActivityTypeYoga:
return @"Yoga";
case HKWorkoutActivityTypeOther:
return @"Other";
case HKWorkoutActivityTypeBarre:
return @"Barre";
case HKWorkoutActivityTypeCoreTraining:
return @"CoreTraining";
case HKWorkoutActivityTypeCrossCountrySkiing:
return @"CrossCountrySkiing";
case HKWorkoutActivityTypeDownhillSkiing:
return @"DownhillSkiing";
case HKWorkoutActivityTypeFlexibility:
return @"Flexibility";
case HKWorkoutActivityTypeHighIntensityIntervalTraining:
return @"HighIntensityIntervalTraining";
case HKWorkoutActivityTypeJumpRope:
return @"JumpRope";
case HKWorkoutActivityTypeKickboxing:
return @"Kickboxing";
case HKWorkoutActivityTypePilates:
return @"Pilates";
case HKWorkoutActivityTypeSnowboarding:
return @"Snowboarding";
case HKWorkoutActivityTypeStairs:
return @"Stairs";
case HKWorkoutActivityTypeStepTraining:
return @"StepTraining";
case HKWorkoutActivityTypeWheelchairWalkPace:
return @"WheelchairWalkPace";
case HKWorkoutActivityTypeWheelchairRunPace:
return @"WheelchairRunPace";
case HKWorkoutActivityTypeTaiChi:
return @"TaiChi";
case HKWorkoutActivityTypeMixedCardio:
return @"MixedCardio";
case HKWorkoutActivityTypeHandCycling:
return @"HandCycling";
default:{
NSException *e = [NSException
exceptionWithName:@"HKWorkoutActivityType InvalidValue"
reason:@"HKWorkoutActivityType can only have a value from the HKWorkoutActivityType enum"
userInfo:nil];
@throw e;
}
}
}
@end
......@@ -3,7 +3,8 @@
// RCTAppleHealthKit
//
// Created by Greg Wilson on 2016-06-26.
// Copyright © 2016 Greg Wilson. All rights reserved.
// This source code is licensed under the MIT-style license found in the
// LICENSE file in the root directory of this source tree.
//
#import <Foundation/Foundation.h>
......@@ -15,9 +16,11 @@
@interface RCTAppleHealthKit : NSObject <RCTBridgeModule>
@property (nonatomic) HKHealthStore *healthStore;
@property BOOL isSync;
- (void)isHealthKitAvailable:(RCTResponseSenderBlock)callback;
- (void)initializeHealthKit:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)checkPermission:(NSString *)input callback:(RCTResponseSenderBlock)callback;
- (void)getModuleInfo:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
@end
......@@ -3,14 +3,17 @@
// RCTAppleHealthKit
//
// Created by Greg Wilson on 2016-06-26.
// Copyright © 2016 Greg Wilson. All rights reserved.
// This source code is licensed under the MIT-style license found in the
// LICENSE file in the root directory of this source tree.
//
#import "RCTAppleHealthKit.h"
#import "RCTAppleHealthKit+TypesAndPermissions.h"
#import "RCTAppleHealthKit+Methods_Activity.h"
#import "RCTAppleHealthKit+Methods_Body.h"
#import "RCTAppleHealthKit+Methods_Fitness.h"
#import "RCTAppleHealthKit+Methods_Dietary.h"
#import "RCTAppleHealthKit+Methods_Characteristic.h"
#import "RCTAppleHealthKit+Methods_Vitals.h"
#import "RCTAppleHealthKit+Methods_Results.h"
......@@ -22,6 +25,7 @@
#import <React/RCTEventDispatcher.h>
@implementation RCTAppleHealthKit
@synthesize bridge = _bridge;
RCT_EXPORT_MODULE();
......@@ -96,11 +100,31 @@ RCT_EXPORT_METHOD(getLatestBodyFatPercentage:(NSDictionary *)input callback:(RCT
[self body_getLatestBodyFatPercentage:input callback:callback];
}
RCT_EXPORT_METHOD(getBodyFatPercentageSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback)
{
[self body_getBodyFatPercentageSamples:input callback:callback];
}
RCT_EXPORT_METHOD(saveBodyFatPercentage:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback)
{
[self body_saveBodyFatPercentage:input callback:callback];
}
RCT_EXPORT_METHOD(getLatestLeanBodyMass:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback)
{
[self body_getLatestLeanBodyMass:input callback:callback];
}
RCT_EXPORT_METHOD(getLeanBodyMassSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback)
{
[self body_getLeanBodyMassSamples:input callback:callback];
}
RCT_EXPORT_METHOD(saveLeanBodyMass:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback)
{
[self body_saveLeanBodyMass:input callback:callback];
}
RCT_EXPORT_METHOD(getStepCount:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback)
{
[self fitness_getStepCountOnDay:input callback:callback];
......@@ -116,6 +140,17 @@ RCT_EXPORT_METHOD(getStepCountSamplesByAnchor:(NSDictionary *)input callback:(RC
[self fitness_geStepSamplesByAnchor:input callback:callback];
}
RCT_EXPORT_METHOD(getSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback)
{
[self fitness_getSamples:input callback:callback];
}
RCT_EXPORT_METHOD(setObserver:(NSDictionary *)input)
{
[self fitness_setObserver:input];
}
RCT_EXPORT_METHOD(getDailyStepCountSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback)
{
[self fitness_getDailyStepSamples:input callback:callback];
......@@ -131,16 +166,46 @@ RCT_EXPORT_METHOD(getDistanceWalkingRunning:(NSDictionary *)input callback:(RCTR
[self fitness_getDistanceWalkingRunningOnDay:input callback:callback];
}
RCT_EXPORT_METHOD(getDailyDistanceWalkingRunningSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback)
{
[self fitness_getDailyDistanceWalkingRunningSamples:input callback:callback];
}
RCT_EXPORT_METHOD(getDailyDistanceSwimmingSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback)
{
[self fitness_getDailyDistanceSwimmingSamples:input callback:callback];
}
RCT_EXPORT_METHOD(getDistanceCycling:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback)
{
[self fitness_getDistanceCyclingOnDay:input callback:callback];
}
RCT_EXPORT_METHOD(getDailyDistanceCyclingSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback)
{
[self fitness_getDailyDistanceCyclingSamples:input callback:callback];
}
RCT_EXPORT_METHOD(getFlightsClimbed:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback)
{
[self fitness_getFlightsClimbedOnDay:input callback:callback];
}
RCT_EXPORT_METHOD(getDailyFlightsClimbedSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback)
{
[self fitness_getDailyFlightsClimbedSamples:input callback:callback];
}
RCT_EXPORT_METHOD(saveFood:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback)
{
[self saveFood:input callback:callback];
}
RCT_EXPORT_METHOD(saveWater:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback)
{
[self saveWater:input callback:callback];
}
RCT_EXPORT_METHOD(getHeartRateSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback)
{
[self vitals_getHeartRateSamples:input callback:callback];
......@@ -151,6 +216,16 @@ RCT_EXPORT_METHOD(getHeartRateSamplesByAnchor:(NSDictionary *)input callback:(RC
[self vitals_getHeartRateSamplesByAnchor:input callback:callback];
}
RCT_EXPORT_METHOD(getActiveEnergyBurned:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback)
{
[self activity_getActiveEnergyBurned:input callback:callback];
}
RCT_EXPORT_METHOD(getBasalEnergyBurned:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback)
{
[self activity_getBasalEnergyBurned:input callback:callback];
}
RCT_EXPORT_METHOD(getBodyTemperatureSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback)
{
[self vitals_getBodyTemperatureSamples:input callback:callback];
......@@ -261,9 +336,7 @@ RCT_EXPORT_METHOD(setTimestamp:(NSDictionary *)input callback:(RCTResponseSender
[self.healthStore requestAuthorizationToShareTypes:writeDataTypes readTypes:readDataTypes completion:^(BOOL success, NSError *error) {
if (!success) {
NSString *errMsg = [NSString stringWithFormat:@"Error with HealthKit authorization: %@", error];
NSLog(errMsg);
callback(@[RCTMakeError(errMsg, nil, nil)]);
callback(@[RCTJSErrorFromNSError(error)]);
return;
} else {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
......@@ -276,6 +349,28 @@ RCT_EXPORT_METHOD(setTimestamp:(NSDictionary *)input callback:(RCTResponseSender
}
}
RCT_EXPORT_METHOD(authorizationStatusForType:(NSString *)type
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject
{
if (self.healthStore == nil) {
self.healthStore = [[HKHealthStore alloc] init];
}
if ([HKHealthStore isHealthDataAvailable]) {
HKObjectType *objectType = [self getWritePermFromString:type];
if (objectType == nil) {
reject(@"unknown write permission", nil, nil);
return;
}
NSString *status = [self getAuthorizationStatusString:[self.healthStore authorizationStatusForType:objectType]];
resolve(status);
} else {
reject(@"HealthKit data is not available", nil, nil);
}
})
- (void)getModuleInfo:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback
{
NSDictionary *info = @{
......
# React Native Apple Healthkit
A React Native bridge module for interacting with [Apple Healthkit] data.
A React Native bridge module for interacting with Apple Healthkit data. Checkout the [full documentation](https://github.com/terrillo/rn-apple-healthkit/tree/master/docs)
## Installation
......@@ -28,7 +28,6 @@ Update `info.plist` in your React Native project
![](https://i.imgur.com/eOCCCyv.png "Xcode Capabilities Section")
7. Compile and run
## Get Started
Initialize Healthkit. This will show the Healthkit permissions prompt for any read/write permissions set in the required `options` object.
......@@ -44,8 +43,8 @@ If new read/write permissions are added to the options object then the app user
```javascript
let options = {
permissions: {
read: ["Height", "Weight", "StepCount", "DateOfBirth", "BodyMassIndex"],
write: ["Weight", "StepCount", "BodyMassIndex"]
read: ["Height", "Weight", "StepCount", "DateOfBirth", "BodyMassIndex", "ActiveEnergyBurned"],
write: ["Height", "Weight", "StepCount", "BodyMassIndex", "Biotin", "Caffeine", "Calcium", "Carbohydrates", "Chloride", "Cholesterol", "Copper", "EnergyConsumed", "FatMonounsaturated", "FatPolyunsaturated", "FatSaturated", "FatTotal", "Fiber", "Folate", "Iodine", "Iron", "Magnesium", "Manganese", "Molybdenum", "Niacin", "PantothenicAcid", "Phosphorus", "Potassium", "Protein", "Riboflavin", "Selenium", "Sodium", "Sugar", "Thiamin", "VitaminA", "VitaminB12", "VitaminB6", "VitaminC", "VitaminD", "VitaminE", "VitaminK", "Zinc", "Water"]
}
};
```
......@@ -77,43 +76,78 @@ AppleHealthKit.initHealthKit(options: Object, (err: string, results: Object) =>
}
```
## Changelog
0.6.5v
- Enable fetching basal energy [#23](https://github.com/terrillo/rn-apple-healthkit/pull/23)
- remove checkPermission functions in order to use from PR [#69](https://github.com/terrillo/rn-apple-healthkit/pull/69)
- Added correct link to permissions. [#73](https://github.com/terrillo/rn-apple-healthkit/pull/73)
- Add unified way to get workouts + convert Activity Types to name + isTracked flag [#25](https://github.com/terrillo/rn-apple-healthkit/pull/25)
0.6.4v
- Basal energy ([#23](https://github.com/terrillo/rn-apple-healthkit/pull/23))
- Fixed issues with saving weight in the past
- Commited the docs to increase pull request support
- Add daily samples for:
- Flights Climbed
- WalkingRunning Distance
- Cycling Distance
0.6.3v
- Food and Water ([#19](https://github.com/terrillo/rn-apple-healthkit/pull/19))
0.6.1v
- HKQuantityTypeIdentifierActiveEnergyBurned
## Wiki
* [Installation](https://github.com/terrillo/rn-apple-healthkit/wiki/Install)
* [Installation](/docs/Install)
* [Documentation](#documentation)
* [Permissions](#permissions)
* [Permissions](#supported-apple-permissions)
* [Units](#units)
* Base Methods
* [isAvailable](https://github.com/terrillo/rn-apple-healthkit/wiki/isAvailable())
* [initHealthKit](https://github.com/terrillo/rn-apple-healthkit/wiki/initHealthKit())
* [isAvailable](/docs/isAvailable().md)
* [initHealthKit](/docs/initHealthKit().md)
* [authorizationStatusForType](/docs/authorizationStatusForType().md)
* Realtime Methods
* [initStepCountObserver](https://github.com/terrillo/rn-apple-healthkit/wiki/initStepCountObserver())
* [initStepCountObserver](/docs/initStepCountObserver().md)
* [setObserver](/docs/setObserver().md)
* Read Methods
* [getBiologicalSex](https://github.com/terrillo/rn-apple-healthkit/wiki/getBiologicalSex())
* [getBloodGlucoseSamples](https://github.com/terrillo/rn-apple-healthkit/wiki/getbloodglucosesamples())
* [getBloodPressureSamples](https://github.com/terrillo/rn-apple-healthkit/wiki/getbloodpressuresamples())
* [getBodyTemperatureSamples](https://github.com/terrillo/rn-apple-healthkit/wiki/getbodytemperaturesamples())
* [getDailyStepCountSamples](https://github.com/terrillo/rn-apple-healthkit/wiki/getDailyStepCountSamples())
* [getDateOfBirth](https://github.com/terrillo/rn-apple-healthkit/wiki/getDateOfBirth())
* [getDistanceCycling](https://github.com/terrillo/rn-apple-healthkit/wiki/getdistancecycling())
* [getDistanceWalkingRunning](https://github.com/terrillo/rn-apple-healthkit/wiki/getDistanceWalkingRunning())
* [getFlightsClimbed](https://github.com/terrillo/rn-apple-healthkit/wiki/getflightsclimbed())
* [getHeartRateSamples](https://github.com/terrillo/rn-apple-healthkit/wiki/getheartratesamples())
* [getHeightSamples](https://github.com/terrillo/rn-apple-healthkit/wiki/getheightsamples())
* [getLatestBmi](https://github.com/terrillo/rn-apple-healthkit/wiki/getlatestbmi())
* [getLatestBodyFatPercentage](https://github.com/terrillo/rn-apple-healthkit/wiki/getlatestbodyfatpercentage())
* [getLatestHeight](https://github.com/terrillo/rn-apple-healthkit/wiki/getlatestheight())
* [getLatestLeanBodyMass](https://github.com/terrillo/rn-apple-healthkit/wiki/getlatestleanbodymass())
* [getLatestWeight](https://github.com/terrillo/rn-apple-healthkit/wiki/getlatestweight())
* [getRespiratoryRateSamples](https://github.com/terrillo/rn-apple-healthkit/wiki/getrespiratoryratesamples())
* [getSleepSamples](https://github.com/terrillo/rn-apple-healthkit/wiki/getsleepsamples())
* [getStepCount](https://github.com/terrillo/rn-apple-healthkit/wiki/getStepCount())
* [getWeightSamples](https://github.com/terrillo/rn-apple-healthkit/wiki/getweightsamples())
* [getActiveEnergyBurned](/docs/getActiveEnergyBurned().md)
* [getBasalEnergyBurned](/docs/getBasalEnergyBurned().md)
* [getBiologicalSex](/docs/getBiologicalSex().md)
* [getBloodGlucoseSamples](/docs/getBloodglucoseSamples().md)
* [getBloodPressureSamples](/docs/getBloodPressureSamples().md)
* [getBodyTemperatureSamples](/docs/getBodyTemperatureSamples().md)
* [getDailyDistanceCyclingSamples](/docs/getDailyDistanceCyclingSamples().md)
* [getDailyDistanceWalkingRunningSamples](/docs/getDailyDistanceWalkingRunningSamples().md)
* [getDailyFlightsClimbedSamples](/docs/getDailyFlightsClimbedSamples().md)
* [getDailyStepCountSamples](/docs/getDailyStepCountSamples().md)
* [getDateOfBirth](/docs/getDateOfBirth().md)
* [getDistanceCycling](/docs/getDistanceCycling().md)
* [getDistanceSwimming](/docs/getDistanceSwimming().md)
* [getDistanceWalkingRunning](/docs/getDistanceWalkingRunning().md)
* [getFlightsClimbed](/docs/getFlightsClimbed().md)
* [getHeartRateSamples](/docs/getHeartRateSamples().md)
* [getHeightSamples](/docs/getHeightSamples().md)
* [getLatestBmi](/docs/getLatestBmi().md)
* [getLatestBodyFatPercentage](/docs/getLatestBodyFatPercentage().md)
* [getBodyFatPercentageSamples](/docs/getBodyFatPercentageSamples().md)
* [getLatestHeight](/docs/getLatestHeight().md)
* [getLatestLeanBodyMass](/docs/getLatestLeanBodyMass().md)
* [getLeanBodyMassSamples](/docs/getLeanBodyMassSamples().md)
* [getLatestWeight](/docs/getLatestWeight().md)
* [getRespiratoryRateSamples](/docs/getRespiratoryRateSamples().md)
* [getSleepSamples](/docs/getSleepSamples().md)
* [getStepCount](/docs/getStepCount().md)
* [getWeightSamples](/docs/getWeightSamples().md)
* [getSamples](docs/getSamples().md)
* Write Methods
* [saveBmi](https://github.com/terrillo/rn-apple-healthkit/wiki/savebmi())
* [saveHeight](https://github.com/terrillo/rn-apple-healthkit/wiki/saveheight())
* [saveMindfulSession](https://github.com/terrillo/rn-apple-healthkit/wiki/saveMindfulSession())
* [saveWeight](https://github.com/terrillo/rn-apple-healthkit/wiki/saveweight())
* [saveSteps](https://github.com/terrillo/rn-apple-healthkit/wiki/saveSteps())
* [saveBmi](/docs/saveBmi().md)
* [saveHeight](/docs/saveHeight().md)
* [saveMindfulSession](/docs/saveMindfulSession().md)
* [saveWeight](/docs/saveWeight().md)
* [saveSteps](/docs/saveSteps().md)
* [saveBodyFatPercentage](/docs/saveBodyFatPercentage().md)
* [saveLeanBodyMass](/docs/saveLeanBodyMass().md)
* [References](#references)
## Supported Apple Permissions
......@@ -122,6 +156,8 @@ The available Healthkit permissions to use with `initHealthKit`
| Permission | Healthkit Identifier Type | Read | Write |
|------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------|------|-------|
| ActiveEnergyBurned | [HKQuantityTypeIdentifierActiveEnergyBurned](https://developer.apple.com/documentation/healthkit/hkquantitytypeidentifier/1615771-activeenergyburned?language=objc) | ✓ | |
| BasalEnergyBurned | [HKQuantityTypeIdentifierBasalEnergyBurned](https://developer.apple.com/documentation/healthkit/hkquantitytypeidentifier/1615512-basalenergyburned?language=objc) | ✓ | |
| BiologicalSex | [HKCharacteristicTypeIdentifierBiologicalSex](https://developer.apple.com/reference/Healthkit/hkcharacteristictypeidentifierbiologicalsex?language=objc) | ✓ | |
| BloodGlucose | [HKQuantityTypeIdentifierBloodGlucose](https://developer.apple.com/reference/Healthkit/hkquantitytypeidentifierbloodglucose?language=objc) | ✓ | |
| BloodPressureDiastolic | [HKQuantityTypeIdentifierBloodPressureDiastolic](https://developer.apple.com/documentation/healthkit/hkquantitytypeidentifierbloodpressurediastolic?language=objc) | ✓ | ✓ |
......@@ -141,6 +177,7 @@ The available Healthkit permissions to use with `initHealthKit`
| StepCount | [HKQuantityTypeIdentifierStepCount](https://developer.apple.com/reference/Healthkit/hkquantitytypeidentifierstepcount?language=objc) | ✓ | ✓ |
| Steps | [HKQuantityTypeIdentifierSteps](https://developer.apple.com/reference/Healthkit/hkquantitytypeidentifiersteps?language=objc) | ✓ | ✓ |
| Weight | [HKQuantityTypeIdentifierBodyMass](https://developer.apple.com/reference/Healthkit/hkquantitytypeidentifierbodymass?language=objc) | ✓ | ✓ |
| BodyFatPercentage | [HKQuantityTypeIdentifierBodyFatPercentage](https://developer.apple.com/reference/Healthkit/hkquantitytypeidentifierbodyfatpercentage?language=objc) | ✓ | ✓ |
These permissions are exported as constants of the `rn-apple-healthkit` module.
......@@ -186,6 +223,7 @@ const healthKitOptions = {
- pound
- second
## References
- Apple Healthkit Documentation [https://developer.apple.com/Healthkit/](https://developer.apple.com/Healthkit/)
......
Install the `rn-apple-healthkit` npm package
- Run `npm install rn-apple-healthkit --save`
- Run `react-native link rn-apple-healthkit`
Update `info.plist` in your React Native project
```
<key>NSHealthShareUsageDescription</key>
<string>Read and understand health data.</string>
<key>NSHealthUpdateUsageDescription</key>
<string>Share workout data with other apps.</string>
```
These permissions are exported as constants of the `rn-apple-healthkit` module.
```javascript
import AppleHealthKit from 'rn-apple-healthkit';
// get the available permissions from AppleHealthKit.Constants object
const PERMS = AppleHealthKit.Constants.Permissions;
// setup healthkit read/write permissions using PERMS
const healthKitOptions = {
permissions: {
read: [
PERMS.StepCount,
PERMS.Height,
],
write: [
PERMS.StepCount
],
}
};
```
\ No newline at end of file
Check the authorization status for sharing (writing) the specified data type.
Status will be one of `"NotDetermined"`, `"SharingDenied"`, `"SharingAuthorized"`.
```javascript
try {
const status = await AppleHealthKit.authorizationStatusForType("StepCount")
if (status) {
console.log("status is", status)
}
} catch (error) {
console.warn(error)
}
```
There is no way to check authorization status for read permission, [see this](https://developer.apple.com/documentation/healthkit/hkhealthstore/1614154-authorizationstatusfortype?language=objc).
A quantity sample type that measures the amount of active energy the user has burned.
```javascript
let d = new Date(2016,1,1);
let options = {
startDate: (new Date(2016,10,1)).toISOString(), // required
endDate: (new Date()).toISOString(), // optional; default now
};
```
```javascript
AppleHealthKit.getActiveEnergyBurned(options: Object, (err: Object, results: Object) => {
if (err) {
return;
}
console.log(results)
});
```
\ No newline at end of file
```javascript
let d = new Date(2016,1,1);
let options = {
startDate: (new Date(2018,10,1)).toISOString(), // required
endDate: (new Date()).toISOString(), // optional; default now
};
```
```javascript
AppleHealthKit.getBasalEnergyBurned(options: Object, (err: Object, results: Object) => {
if (err) {
return;
}
console.log(results)
});
```
Get the biological sex (gender). If the `BiologicalSex` read permission is missing or the user has denied it then the value will be `unknown`. The possible values are:
| Value | HKBiologicalSex |
|---------|-----------------------|
| unknown | HKBiologicalSexNotSet |
| male | HKBiologicalSexMale |
| female | HKBiologicalSexFemale |
| other | HKBiologicalSexOther |
```javascript
AppleHealthKit.getBiologicalSex(null, (err: Object, results: Object) => {
if (this._handleHealthkitError(err, 'getBiologicalSex')) {
return;
}
console.log(results)
});
```
```javascript
{
value: 'female',
}
```
\ No newline at end of file
Query for blood glucose samples. the options object is used to setup a query to retrieve relevant samples.
```javascript
let options = {
unit: 'mmolPerL', // optional; default 'mmolPerL'
startDate: (new Date(2016,4,27)).toISOString(), // required
endDate: (new Date()).toISOString(), // optional; default now
ascending: false, // optional; default false
limit:10, // optional; default no limit
};
```
Available units are: `'mmolPerL'`, `'mgPerdL'`.
The callback function will be called with a `samples` array containing objects with *value*, *sourceId*, *sourceName*, *startDate*, and *endDate* fields
```javascript
AppleHealthKit.getBloodGlucoseSamples(options, (err: Object, results: Array<Object>) => {
if (err) {
return;
}
console.log(results)
});
```
Query for blood pressure samples. the options object is used to setup a query to retrieve relevant samples.
```javascript
let options = {
unit: 'mmhg', // optional; default 'mmhg'
startDate: (new Date(2016,4,27)).toISOString(), // required
endDate: (new Date()).toISOString(), // optional; default now
ascending: false, // optional; default false
limit:10, // optional; default no limit
};
```
The callback function will be called with a `samples` array containing objects with *bloodPressureSystolicValue*, *bloodPressureDiastolicValue*, *startDate*, and *endDate* fields
```javascript
AppleHealthKit.getBloodPressureSamples(options, (err: Object, results: Array<Object>) => {
if (err) {
return;
}
console.log(results)
});
```
```javascript
[
{ bloodPressureSystolicValue: 120, bloodPressureDiastolicValue: 81, startDate:'2016-06-29T17:55:00.000-0400', endDate:'2016-06-29T17:55:00.000-0400' },
{ bloodPressureSystolicValue: 119, bloodPressureDiastolicValue: 77, startDate:'2016-03-12T13:22:00.000-0400', endDate:'2016-03-12T13:22:00.000-0400' },
]
```
\ No newline at end of file
Query for body fat percentage samples. the options object is used to setup a query to retrieve relevant samples.
```javascript
let options = {
startDate: (new Date(2016,4,27)).toISOString(), // required
endDate: (new Date()).toISOString(), // optional; default now
ascending: false, // optional; default false
limit:10, // optional; default no limit
};
```
```javascript
AppleHealthKit.getBodyFatPercentageSamples(options, (err: Object, results: Array<Object>) => {
if (err) {
return;
}
console.log(results)
});
```
```javascript
[
{ value: 16.5, startDate: '2016-07-09T00:00:00.000-0400', endDate: '2016-07-10T00:00:00.000-0400' },
{ value: 16.1, startDate: '2016-07-08T00:00:00.000-0400', endDate: '2016-07-09T00:00:00.000-0400' },
{ value: 15.9, startDate: '2016-07-07T00:00:00.000-0400', endDate: '2016-07-08T00:00:00.000-0400' },
]
```
Query for body temperature samples. the options object is used to setup a query to retrieve relevant samples.
```javascript
let options = {
unit: 'celsius', // optional; default 'celsius'
startDate: (new Date(2016,4,27)).toISOString(), // required
endDate: (new Date()).toISOString(), // optional; default now
ascending: false, // optional; default false
limit:10, // optional; default no limit
};
```
Available units are: `'fahrenheit'`, `'celsius'`.
The callback function will be called with a `samples` array containing objects with *value*, *startDate*, and *endDate* fields.
```javascript
AppleHealthKit.getBodyTemperatureSamples(options, (err: Object, results: Array<Object>) => {
if (err) {
return;
}
console.log(results)
});
```
```javascript
[
{ value: 74.02, startDate:'2016-06-29T17:55:00.000-0400', endDate:'2016-06-29T17:55:00.000-0400' },
{ value: 74, startDate:'2016-03-12T13:22:00.000-0400', endDate:'2016-03-12T13:22:00.000-0400' },
]
```
\ No newline at end of file
```javascript
let options = {
startDate: (new Date(2016,4,27)).toISOString(), // required
endDate: (new Date()).toISOString(), // optional; default now
ascending: false, // optional; default false
limit:10, // optional; default no limit
};
```
```javascript
AppleHealthKit.getDailyDistanceCyclingSamples(options, (err: Object, results: Array<Object>) => {
if (err) {
return;
}
console.log(results)
});
```
```javascript
let options = {
startDate: (new Date(2016,4,27)).toISOString(), // required
endDate: (new Date()).toISOString(), // optional; default now
ascending: false, // optional; default false
limit:10, // optional; default no limit
period: 60, // time interval in minutes optional: default 60
includeManuallyAdded: false. // optional: default false
};
```
```javascript
AppleHealthKit.getDailyDistanceSwimmingSamples(options, (err: Object, results: Array<Object>) => {
if (err) {
return;
}
console.log(results)
});
```
```javascript
let options = {
startDate: (new Date(2016,4,27)).toISOString(), // required
endDate: (new Date()).toISOString(), // optional; default now
ascending: false, // optional; default false
limit:10, // optional; default no limit
};
```
```javascript
AppleHealthKit.getDailyDistanceWalkingRunningSamples(options, (err: Object, results: Array<Object>) => {
if (err) {
return;
}
console.log(results)
});
```
```javascript
let options = {
startDate: (new Date(2016,4,27)).toISOString(), // required
endDate: (new Date()).toISOString(), // optional; default now
ascending: false, // optional; default false
limit:10, // optional; default no limit
};
```
```javascript
AppleHealthKit.getDailyFlightsClimbedSamples(options, (err: Object, results: Array<Object>) => {
if (err) {
return;
}
console.log(results)
});
```
Get the total steps per day over a specified date range.
`getDailyStepCountSamples` accepts an options object containing required *`startDate: ISO8601Timestamp`* and optional *`endDate: ISO8601Timestamp`*. If `endDate` is not provided it will default to the current time
```javascript
let options = {
startDate: (new Date(2016,1,1)).toISOString() // required
endDate: (new Date()).toISOString() // optional; default now
};
```
```javascript
AppleHealthKit.getDailyStepCountSamples(options: Object, (err: Object, results: Array<Object>) => {
if (this._handleHealthkitError(err, 'getDailyStepCountSamples')) {
return;
}
console.log(results)
});
```
\ No newline at end of file
Get the date of birth.
On success, the callback function will be provided with a `res` object containing dob `value: string` (ISO timestamp), and `age: number` (age in years):
```javascript
AppleHealthKit.getDateOfBirth(null, (err: Object, results: Object) => {
if (this._handleHealthkitError(err, 'getDateOfBirth')) {
return;
}
console.log(results)
});
```
```javascript
{
value: '1986-09-01T00:00:00.000-0400',
age: 29
}
```
\ No newline at end of file
Get the total distance cycling on a specific day.
`getDistanceCycling` accepts an options object containing optional *`date: ISO8601Timestamp`* and *`unit: string`*. If `date` is not provided it will default to the current time. `unit` defaults to `meter`
```javascript
let options = {
unit: 'mile', // optional; default 'meter'
date: (new Date(2016,5,1)).toISOString(), // optional; default now
};
```
```javascript
AppleHealthKit.getDistanceCycling(options: Object, (err: Object, results: Object) => {
if (err) {
return;
}
console.log(results)
});
```
```javascript
{
value: 11.45,
startDate: '2016-07-08T12:00:00.000-0400',
endDate: '2016-07-08T12:00:00.000-0400'
}
```
\ No newline at end of file
Get the total distance walking/running on a specific day.
`getDistanceWalkingRunning` accepts an options object containing optional *`date: ISO8601Timestamp`* and *`unit: string`*. If `date` is not provided it will default to the current time. `unit` defaults to `meter`.
```javascript
let options = {
unit: 'mile', // optional; default 'meter'
date: (new Date(2016,5,1)).toISOString(), // optional; default now
};
```
```javascript
AppleHealthKit.getDistanceWalkingRunning(options: Object, (err: Object, results: Object) => {
if (err) {
return;
}
console.log(results)
});
```
```javascript
{
value: 1.45,
startDate: '2016-07-08T12:00:00.000-0400',
endDate: '2016-07-08T12:00:00.000-0400'
}
```
\ No newline at end of file
get the total flights climbed (1 flight is ~10ft of elevation) on a specific day.
`getFlightsClimbed` accepts an options object containing optional *`date: ISO8601Timestamp`*. if `date` is not provided it will default to the current time.
```javascript
let options = {
date: (new Date(2016,5,1)).toISOString(), // optional; default now
};
```
```javascript
AppleHealthKit.getFlightsClimbed(options: Object, (err: Object, results: Object) => {
if (err) {
return;
}
console.log(results)
});
```
```javascript
{
value: 15,
startDate: '2016-07-08T12:00:00.000-0400',
endDate: '2016-07-08T12:00:00.000-0400'
}
```
\ No newline at end of file
Query for heart rate samples. the options object is used to setup a query to retrieve relevant samples.
```javascript
let options = {
unit: 'bpm', // optional; default 'bpm'
startDate: (new Date(2016,4,27)).toISOString(), // required
endDate: (new Date()).toISOString(), // optional; default now
ascending: false, // optional; default false
limit:10, // optional; default no limit
};
```
The callback function will be called with a `samples` array containing objects with *value*, *startDate*, and *endDate* fields
```javascript
AppleHealthKit.getHeartRateSamples(options, (err: Object, results: Array<Object>) => {
if (err) {
return;
}
console.log(results)
});
```
```javascript
[
{ value: 74.02, startDate:'2016-06-29T17:55:00.000-0400', endDate:'2016-06-29T17:55:00.000-0400' },
{ value: 74, startDate:'2016-03-12T13:22:00.000-0400', endDate:'2016-03-12T13:22:00.000-0400' },
]
\ No newline at end of file
query for height samples. the options object is used to setup a query to retrieve relevant samples.
```javascript
let options = {
unit: 'inch', // optional; default 'inch'
startDate: (new Date(2016,4,27)).toISOString(), // required
endDate: (new Date()).toISOString(), // optional; default now
ascending: false, // optional; default false
limit:10, // optional; default no limit
};
```
```javascript
AppleHealthKit.getHeightSamples(options, (err: Object, results: Array<Object>) => {
if (err) {
return;
}
console.log(results)
});
```
The callback function will be called with a `samples` array containing objects with `value`, `startDate`, and `endDate` fields
```javascript
[
{ value: 74.02, startDate:'2016-06-29T17:55:00.000-0400', endDate:'2016-06-29T17:55:00.000-0400' },
{ value: 74, startDate:'2016-03-12T13:22:00.000-0400', endDate:'2016-03-12T13:22:00.000-0400' },
]
```
\ No newline at end of file
Get the most recent BMI sample.
On success, the callback function will be provided with a `bmi` object containing the BMI `value`, and the `startDate` and `endDate` of the sample. *Note: startDate and endDate will be the same as bmi samples are saved at a specific point in time.*
```javascript
AppleHealthKit.getLatestBmi(null, (err: string, results: Object) => {
if (err) {
console.log("error getting latest bmi data: ", err);
return;
}
console.log(results)
});
```
```javascript
{
value: 27.2,
startDate: '2016-07-08T12:00:00.000-0400',
endDate: '2016-07-08T12:00:00.000-0400'
}
```
Get the most recent body fat percentage. The percentage value is a number between 0 and 100.
On success, the callback function will be provided with a `bodyFatPercentage` object containing the body fat percentage `value`, and the `startDate` and `endDate` of the sample. *Note: startDate and endDate will be the same as bodyFatPercentage samples are saved at a specific point in time.*
```javascript
AppleHealthKit.getLatestBodyFatPercentage(null, (err: Object, results: Object) => {
if (err) {
return;
}
console.log(results)
});
```
```javascript
{
value: 20,
startDate: '2016-07-08T12:00:00.000-0400',
endDate: '2016-07-08T12:00:00.000-0400'
}
```
\ No newline at end of file
Get the most recent height value.
On success, the callback function will be provided with a `height` object containing the height `value`, and the `startDate` and `endDate` of the height sample. *Note: startDate and endDate will be the same as height samples are saved at a specific point in time.*
```javascript
AppleHealthKit.getLatestHeight(null, (err: string, results: Object) => {
if (err) {
console.log("error getting latest height: ", err);
return;
}
console.log(results)
});
```
```javascript
{
value: 72,
startDate: '2016-07-08T12:00:00.000-0400',
endDate: '2016-07-08T12:00:00.000-0400'
}
```
\ No newline at end of file
Get the most recent lean body mass. The value is a number representing the weight in pounds (lbs)
On success, the callback function will be provided with a `leanBodyMass` object containing the leanBodyMass `value`, and the `startDate` and `endDate` of the sample. *Note: startDate and endDate will be the same as leanBodyMass samples are saved at a specific point in time.*
```javascript
AppleHealthKit.getLatestLeanBodyMass(null, (err: Object, results: Object) => {
if (err) {
return;
}
console.log(results)
});
```
```javascript
{
value: 176,
startDate: '2016-07-08T12:00:00.000-0400',
endDate: '2016-07-08T12:00:00.000-0400'
}
```
\ No newline at end of file
Get the most recent weight sample.
On success, the callback function will be provided with a `weight` object containing the weight `value`, and the `startDate` and `endDate` of the weight sample. *Note: startDate and endDate will be the same as weight samples are saved at a specific point in time.*
```javascript
let options = {
unit: 'pound'
};
```
```javascript
AppleHealthKit.getLatestWeight(options, (err: string, results: Object) => {
if (err) {
console.log("error getting latest weight: ", err);
return;
}
console.log(results)
});
```
```javascript
{
value: 200,
startDate: '2016-07-08T12:00:00.000-0400',
endDate: '2016-07-08T12:00:00.000-0400'
}
```
Query for lean body mass samples. the options object is used to setup a query to retrieve relevant samples.
```javascript
let options = {
unit: 'pound', // optional; default 'pound'
startDate: (new Date(2016,4,27)).toISOString(), // required
endDate: (new Date()).toISOString(), // optional; default now
ascending: false, // optional; default false
limit:10, // optional; default no limit
};
```
```javascript
AppleHealthKit.getLeanBodyMassSamples(options, (err: Object, results: Array<Object>) => {
if (err) {
return;
}
console.log(results)
});
```
```javascript
[
{ value: 160, startDate: '2016-07-09T00:00:00.000-0400', endDate: '2016-07-10T00:00:00.000-0400' },
{ value: 161, startDate: '2016-07-08T00:00:00.000-0400', endDate: '2016-07-09T00:00:00.000-0400' },
{ value: 165, startDate: '2016-07-07T00:00:00.000-0400', endDate: '2016-07-08T00:00:00.000-0400' },
]
```
Query for respiratory rate samples. the options object is used to setup a query to retrieve relevant samples.
```javascript
let options = {
unit: 'bpm', // optional; default 'bpm'
startDate: (new Date(2016,4,27)).toISOString(), // required
endDate: (new Date()).toISOString(), // optional; default now
ascending: false, // optional; default false
limit:10, // optional; default no limit
};
```
The callback function will be called with a `samples` array containing objects with *value*, *startDate*, and *endDate* fields
```javascript
AppleHealthKit.getRespiratoryRateSamples(options, (err: Object, results: Array<Object>) => {
if (err) {
return;
}
console.log(results)
});
```
Query to get all activities of given type with extended information about it.
```javascript 1.7
let options = {
startDate: (new Date(2016,4,27)).toISOString(),
endDate: (new Date()).toISOString(),
type: 'Walking', // one of: ['Walking', 'StairClimbing', 'Running', 'Cycling', 'Workout']
};
```
The callback function will be called with a `samples` array containing objects with *value*, *startDate*, and *endDate* fields
```javascript 1.7
AppleHealthKit.getSamples(options, (err: Object, results: Array<Object>) => {
if (err) {
return;
}
console.log(results)
});
```
Resulting object has different fields for different types.
In case of workout:
```
{
activityId: Number, // [NSNumber numberWithInt:[sample workoutActivityType]]
activityName: Number, // [RCTAppleHealthKit stringForHKWorkoutActivityType:[sample workoutActivityType]]
calories: Number, // [[sample totalEnergyBurned] doubleValueForUnit:[HKUnit kilocalorieUnit]]
tracked: Boolean, // [[sample metadata][HKMetadataKeyWasUserEntered] intValue] !== 1
sourceName: String, // [[[sample sourceRevision] source] name]
sourceId: String, // [[[sample sourceRevision] source] bundleIdentifier]
device: String, // [[sample sourceRevision] productType] or 'iPhone'
distance: Number, // [[sample totalDistance] doubleValueForUnit:[HKUnit mileUnit]]
start: String, // [RCTAppleHealthKit buildISO8601StringFromDate:sample.startDate];
end: String, // [RCTAppleHealthKit buildISO8601StringFromDate:sample.endDate];
}
```
for other types:
```
{
tracked: Boolean, // [[sample metadata][HKMetadataKeyWasUserEntered] intValue] !== 1
sourceName: String, // [[[sample sourceRevision] source] name]
sourceId: String, // [[[sample sourceRevision] source] bundleIdentifier]
device: String, // [[sample sourceRevision] productType] or 'iPhone'
start: String, // [RCTAppleHealthKit buildISO8601StringFromDate:sample.startDate];
end: String, // [RCTAppleHealthKit buildISO8601StringFromDate:sample.endDate];
//based on required type, one of the following will be present.
distance: Number, // [[sample totalDistance] doubleValueForUnit:[HKUnit mileUnit]]
calories: Number, // [[sample totalEnergyBurned] doubleValueForUnit:[HKUnit kilocalorieUnit]]
}
```
Query for sleep samples.
Each sleep sample represents a period of time with a startDate and an endDate.
the sample's value will be either `INBED` or `ASLEEP`. these values should overlap,
meaning that two (or more) samples represent a single nights sleep activity. see
[Healthkit SleepAnalysis] reference documentation
The options object is used to setup a query to retrieve relevant samples.
The options must contain `startDate` and may also optionally include `endDate`
and `limit` options
```javascript
let options = {
startDate: (new Date(2016,10,1)).toISOString(), // required
endDate: (new Date()).toISOString(), // optional; default now
limit:10, // optional; default no limit
};
```
The callback function will be called with a `samples` array containing objects
with *value*, *startDate*, and *endDate* fields
```javascript
AppleHealthKit.getSleepSamples(options, (err: Object, results: Array<Object>) => {
if (err) {
return;
}
console.log(results).
});
```
\ No newline at end of file
Get the aggregated total steps for a specific day (starting and ending at midnight).
An optional options object may be provided containing `date` field representing the selected day. If `date` is not set or an options object is not provided then the current day will be used.
```javascript
let d = new Date(2016,1,1);
let options = {
date: d.toISOString()
};
```
```javascript
AppleHealthKit.getStepCount(options: Object, (err: Object, results: Object) => {
if (err) {
return;
}
console.log(results)
});
```
```javascript
{
value: 213,
}
```
\ No newline at end of file
Query for weight samples. the options object is used to setup a query to retrieve relevant samples.
```javascript
let options = {
unit: 'pound', // optional; default 'pound'
startDate: (new Date(2016,4,27)).toISOString(), // required
endDate: (new Date()).toISOString(), // optional; default now
ascending: false, // optional; default false
limit:10, // optional; default no limit
};
```
```javascript
AppleHealthKit.getWeightSamples(options, (err: Object, results: Array<Object>) => {
if (err) {
return;
}
console.log(results)
});
```
```javascript
[
{ value: 160, startDate: '2016-07-09T00:00:00.000-0400', endDate: '2016-07-10T00:00:00.000-0400' },
{ value: 161, startDate: '2016-07-08T00:00:00.000-0400', endDate: '2016-07-09T00:00:00.000-0400' },
{ value: 165, startDate: '2016-07-07T00:00:00.000-0400', endDate: '2016-07-08T00:00:00.000-0400' },
]
```
\ No newline at end of file
Initialize Healthkit. This will show the Healthkit permissions prompt for any read/write permissions set in the required `options` object.
Due to Apple's privacy model if an app user has previously denied a specific permission then they can not be prompted again for that same permission. The app user would have to go into the Apple Health app and grant the permission to your react-native app under *sources* tab.
For any data that is read from Healthkit the status/error is the same for both. This privacy restriction results in having no knowledge of whether the permission was denied (make sure it's added to the permissions options object), or the data for the specific request was nil (ex. no steps recorded today).
For any data written to Healthkit an authorization error can be caught. If an authorization error occurs you can prompt the user to set the specific permission or add the permission to the options object if not present.
If new read/write permissions are added to the options object then the app user will see the Healthkit permissions prompt with the new permissions to allow.
`initHealthKit` requires an options object with Healthkit permission settings
```javascript
let options = {
permissions: {
read: ["Height", "Weight", "StepCount", "DateOfBirth", "BodyMassIndex"],
write: ["Weight", "StepCount", "BodyMassIndex"]
}
};
```
```javascript
AppleHealthKit.initHealthKit(options: Object, (err: string, results: Object) => {
if (err) {
console.log("error initializing Healthkit: ", err);
return;
}
// Healthkit is initialized...
// now safe to read and write Healthkit data...
});
```
\ No newline at end of file
Setup an HKObserverQuery for step count (HKQuantityTypeIdentifierStepCount) that will
trigger an event listenable on react-native `NativeAppEventEmitter` when the
Healthkit step count has changed.
The `initStepCountObserver` method must be called before adding a listener to
NativeAppEventEmitter. After the step count observer has been initialized you can
listen to the NativeAppEventEmitter `change:steps` event and re-fetch relevent
step count data in the event handler.
The `initStepCountObserver` method should be called after Healthkit has been
successfully initialized (AppleHealthKit.initHealthKit has been called without
error).
```javascript
// import NativeAppEventEmitter from react-native
import {
Navigator,
View,
NativeAppEventEmitter,
} from 'react-native';
```
```javascript
AppleHealthKit.initHealthKit(HKOPTIONS, (err, res) => {
if (this._handleHKError(err, 'initHealthKit')) {
return;
}
// initialize the step count observer
AppleHealthKit.initStepCountObserver({}, () => {});
// add event listener for 'change:steps' and handle the
// event in the event handler function.
//
// when adding a listener, a 'subscription' object is
// returned that must be used to remove the listener
// when the component unmounts. The subscription object
// must be accessible to any function/method/instance
// that will be unsubscribing from the event.
this.sub = NativeAppEventEmitter.addListener(
'change:steps',
(evt) => {
// a 'change:steps' event has been received. step
// count data should be re-fetched from Healthkit.
this._fetchStepCountData();
}
);
// other tasks to perform after Healthkit has been
// initialized (fetch relevant Healthkit data).
this._fetchStepCountData();
this._fetchOtherRelevantHealthkitData();
// ...
});
...
// when the component where the listener was added unmounts
// (or whenever the listener should be removed), call the
// 'remove' method of the subscription object.
componentWillUnmount() {
this.sub.remove();
}
```
Check for Healthkit availability
```javascript
import AppleHealthKit from 'rn-apple-healthkit';
AppleHealthKit.isAvailable((err: Object, available: boolean) => {
if (err) {
console.log("error initializing Healthkit: ", err);
return;
}
// Healthkit is available
});
```
\ No newline at end of file
save a numeric BMI value to Healthkit
`saveBmi` accepts an options object containing a numeric BMI value:
```javascript
let options = {
value: 27.2
}
```
```javascript
AppleHealthKit.saveBmi(options: Object, (err: Object, results: Object) => {
if (err) {
return;
}
// BMI successfully saved
});
```
\ No newline at end of file
save a percentage body fat value to Healthkit
`saveBodyFatPercentage` accepts an options object containing a percent value:
```javascript
let options = {
value: 16.7 // 16.7%
}
```
```javascript
AppleHealthKit.saveBodyFatPercentage(options: Object, (err: Object, results: Object) => {
if (err) {
return;
}
// body fat percentage successfully saved
});
```
save a numeric height value to Healthkit
`saveHeight` accepts an options object containing a numeric height value:
```javascript
let options = {
value: 200 // Inches
}
```
```javascript
AppleHealthKit.saveHeight(options: Object, (err: Object, results: Object) => {
if (err) {
return;
}
// height successfully saved
});
```
\ No newline at end of file
save a numeric lean body mass value to Healthkit
`saveLeanBodyMass` accepts an options object containing a numeric weight value:
```javascript
let options = {
value: 155.6 // lbs
}
```
```javascript
AppleHealthKit.saveLeanBodyMass(options: Object, (err: Object, results: Object) => {
if (err) {
console.log("error saving lean body mass to Healthkit: ", err);
return;
}
// Done
});
```
Each mindfulness sample represents a period of time with a startDate and an endDate.
the options must contain `startDate` and `endDate`
```javascript
let options = {
startDate: (new Date(2016,10,1)).toISOString(), // required
endDate: (new Date()).toISOString(), // optional; default now
};
```
```
AppleHealthKit.saveMindfulSession(options, (err, res) => {
if (err) return {
return
}
console.log('Mindful session saved')
});
```
\ No newline at end of file
Save a step count sample.
A step count sample represents the number of steps during a specific period of time. A sample should be a precise as possible, with startDate and endDate representing the range of time the steps were taken in.
`saveSteps` accepts an options object containing required *`value: number`*, *`startDate: ISO8601Timestamp`*, and *`endDate: ISO8601Timestamp`*.
```javascript
// startDate and endDate are 30 minutes apart.
// this means the step count value occurred within those 30 minutes.
let options = {
value: 100,
startDate: (new Date(2016,6,2,6,0,0)).toISOString(),
endDate: (new Date(2016,6,2,6,30,0)).toISOString()
};
```
```javascript
AppleHealthKit.saveSteps(options, (err, res) => {
if (this._handleHKError(err, 'saveSteps')) {
return;
}
// step count sample successfully saved
});
```
\ No newline at end of file
save a numeric weight value to Healthkit
`saveWeight` accepts an options object containing a numeric weight value:
```javascript
let options = {
value: 200
}
```
```javascript
AppleHealthKit.saveWeight(options: Object, (err: Object, results: Object) => {
if (err) {
console.log("error saving weight to Healthkit: ", err);
return;
}
// Done
});
```
\ No newline at end of file
Will listen for any updates in a given type data in healthKit and call app.
type - one of the `['Walking', 'StairClimbing', 'Running', 'Cycling', 'Workout']`
```javascript 1.8
import { NativeAppEventEmitter } from 'react-native';
//...//
AppleHealthKit.setObserver({ type: 'Walking' });
NativeAppEventEmitter.addListener(
'observer',
callback
);
```
So, callback would be call when new data of given type appears. When it happens, in order to get new info
need to call getSamples() function with proper arguments.
{
"name": "rn-apple-healthkit",
"version": "0.6.0",
"version": "0.6.5",
"description": "A React Native package for interacting with Apple HealthKit",
"main": "index.js",
"repository": {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment