diff --git a/Constants/Permissions.js b/Constants/Permissions.js index 927fe4b33fd449c5a96b1580907d9db1232eea1b..d54f0b8559464e90ff7893b2963592b259c860af 100644 --- a/Constants/Permissions.js +++ b/Constants/Permissions.js @@ -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" } diff --git a/RCTAppleHealthKit.podspec b/RCTAppleHealthKit.podspec new file mode 100644 index 0000000000000000000000000000000000000000..4b886963571ea456f7573e91e54714097d3a1364 --- /dev/null +++ b/RCTAppleHealthKit.podspec @@ -0,0 +1,15 @@ +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 diff --git a/RCTAppleHealthKit.xcodeproj/project.pbxproj b/RCTAppleHealthKit.xcodeproj/project.pbxproj index 51ca4183b8a5be5887e74bcc6c688bcb776573cf..a25cec9ba73c8ba13e174f79fe71091184987efd 100644 --- a/RCTAppleHealthKit.xcodeproj/project.pbxproj +++ b/RCTAppleHealthKit.xcodeproj/project.pbxproj @@ -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 = ""; }; 37837E7B1DCFE270000201A0 /* RCTAppleHealthKit+Methods_Sleep.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "RCTAppleHealthKit+Methods_Sleep.h"; sourceTree = ""; }; 37837E7C1DCFE270000201A0 /* RCTAppleHealthKit+Methods_Sleep.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "RCTAppleHealthKit+Methods_Sleep.m"; sourceTree = ""; }; + 58C81E6D1F84F6970005DD48 /* RCTAppleHealthKit+Methods_Activity.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "RCTAppleHealthKit+Methods_Activity.m"; sourceTree = ""; }; + 58C81E6E1F84F6970005DD48 /* RCTAppleHealthKit+Methods_Activity.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "RCTAppleHealthKit+Methods_Activity.h"; sourceTree = ""; }; 61232F911E303758000A5026 /* RCTAppleHealthKit+Methods_Mindfulness.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "RCTAppleHealthKit+Methods_Mindfulness.h"; sourceTree = ""; }; 61232F921E303758000A5026 /* RCTAppleHealthKit+Methods_Mindfulness.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "RCTAppleHealthKit+Methods_Mindfulness.m"; sourceTree = ""; }; 64C42D481D351A8800A0A8F7 /* RCTAppleHealthKit+Methods_Vitals.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "RCTAppleHealthKit+Methods_Vitals.h"; sourceTree = ""; }; @@ -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; }; diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Activity.h b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Activity.h new file mode 100644 index 0000000000000000000000000000000000000000..5bdecf42dae60369706c726186725b5fd8e47b2d --- /dev/null +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Activity.h @@ -0,0 +1,17 @@ +// +// 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 diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Activity.m b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Activity.m new file mode 100644 index 0000000000000000000000000000000000000000..378413ea44949a59a50da7f271b5b89ff098fbd0 --- /dev/null +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Activity.m @@ -0,0 +1,74 @@ +// +// 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 diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.h b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.h index 472829cae1d16bb51f837b305f5a36514a4109c2..51b677164f95425856626796166d976036fe723d 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.h +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.h @@ -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 diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.m b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.m index cca68ab455b48833f3b9123a98a15b77ca7d9965..141b73518dc3f411d47443c6c0666a7f6c95b412 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.m @@ -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 diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Characteristic.h b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Characteristic.h index 6c0adcceee71627374b47e04caa5ed5ec94abf0b..0909ebef17f19105c9d6af5ef535f8bc757f6993 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Characteristic.h +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Characteristic.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.h" diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Characteristic.m b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Characteristic.m index b7fb4f4226358f084889880784e6ff58a576e3c6..e6266a9325a3d6113f83999c8be36002a5ec118c 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Characteristic.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Characteristic.m @@ -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) { diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Dietary.h b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Dietary.h new file mode 100644 index 0000000000000000000000000000000000000000..f1477ff6be5331055564e82005068cbca1c0a386 --- /dev/null +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Dietary.h @@ -0,0 +1,16 @@ +// +// 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 diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Dietary.m b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Dietary.m new file mode 100644 index 0000000000000000000000000000000000000000..490e29548705e438b7e6e1685d3742659992cd4f --- /dev/null +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Dietary.m @@ -0,0 +1,409 @@ +// +// 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 +#import + +@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 diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.h b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.h index e358b9e142a4a26916c95a3474beb6d9189cda96..31e322b96b128f392031e31b59c2ffdf147dbdb6 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.h +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.h @@ -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 diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.m b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.m index c084375d920e63d634d9ead15c75220714d65aa8..a25ebdbb409ad69b32e83b9058c0d9be4d7bd6d4 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.m @@ -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 diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Mindfulness.m b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Mindfulness.m index b14ef8137a7bc7618f8de087aaf906137d3a0a46..1dc6dca911494dd8dbed44a365ae6e14617bcfc7 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Mindfulness.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Mindfulness.m @@ -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)]); diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Results.m b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Results.m index fd5d0345ceb2278a59782f10b15913de0f3dc434..a1c1cdea6e96786add9f59ca7e190f3e6a18a7b5 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Results.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Results.m @@ -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; } }]; diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Sleep.h b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Sleep.h index 2ddbd5bf678288ec4173e7dbd86a9eb1412a2d71..0a0b7e791369b19559223d562cdba9d074f13735 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Sleep.h +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Sleep.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. // #import "RCTAppleHealthKit.h" diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Sleep.m b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Sleep.m index ecdd062ceca95161f7711e7edca52783d1656b3e..2ab061d9eb6f124aba1e2def0cf172d01d079955 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Sleep.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Sleep.m @@ -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; } }]; diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Vitals.m b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Vitals.m index 26e199403b0a83c23db54bdb329bf1db2676ee41..25446679240559fd3b5391539d3fc49d85ebb0f2 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Vitals.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Vitals.m @@ -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; } }]; diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Queries.h b/RCTAppleHealthKit/RCTAppleHealthKit+Queries.h index 6531a0fa4bfb71ba8121b96763c07b6bb085ec83..af6040c762daae091704e7bd23eb49bd24f3f947 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Queries.h +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Queries.h @@ -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 diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Queries.m b/RCTAppleHealthKit/RCTAppleHealthKit+Queries.m index 1c2a7830dc6057f9132716c2a7ce99efd4a8844f..f42b9c0d04ecb41dad2b747cbb40a5ecfb779e1d 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Queries.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Queries.m @@ -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 +#import +@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]; diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+TypesAndPermissions.h b/RCTAppleHealthKit/RCTAppleHealthKit+TypesAndPermissions.h index 73438fe45b41c11bc4fdd3e28277b9f7d3b0d96c..c6668c69654043f6727b54f1df1c457d82e85caf 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+TypesAndPermissions.h +++ b/RCTAppleHealthKit/RCTAppleHealthKit+TypesAndPermissions.h @@ -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 diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+TypesAndPermissions.m b/RCTAppleHealthKit/RCTAppleHealthKit+TypesAndPermissions.m index b22e6f346770c9153c0223fb5006e186592aff6f..a54f03fb80a47d3030312b714e74f225ecefdcd1 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+TypesAndPermissions.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit+TypesAndPermissions.m @@ -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 diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Utils.h b/RCTAppleHealthKit/RCTAppleHealthKit+Utils.h index 738129fe1c8b0859e093ef60e042274cd98671f3..8673ea52927d48faa725157b3ce2890414a6e239 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Utils.h +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Utils.h @@ -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; diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Utils.m b/RCTAppleHealthKit/RCTAppleHealthKit+Utils.m index 0385e3e65482288aa11cd74ea9426f82cb3f753c..5bb2e7f307bfe06b00637244740b2f392f2e1435 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Utils.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Utils.m @@ -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 diff --git a/RCTAppleHealthKit/RCTAppleHealthKit.h b/RCTAppleHealthKit/RCTAppleHealthKit.h index 39b603415c06570ff35b15f7a7c63b7e44b37606..6ccbe26b77a1fff4840ea61871c77deff969020b 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit.h +++ b/RCTAppleHealthKit/RCTAppleHealthKit.h @@ -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 @@ -15,9 +16,11 @@ @interface RCTAppleHealthKit : NSObject @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 diff --git a/RCTAppleHealthKit/RCTAppleHealthKit.m b/RCTAppleHealthKit/RCTAppleHealthKit.m index 5ab849251c62dcf947caf7194b947223ddc066e4..953d08739d3c328d4b151f6d7cb932ff15c598c0 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit.m @@ -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 @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 = @{ diff --git a/README.md b/README.md index 4a1bb998796775a3bdb01bc448b216c140dbad34..6ad3ead0b94a3ba6b0b69298479f6a2b39a8495c 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # 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/) diff --git a/docs/Installation.md b/docs/Installation.md new file mode 100644 index 0000000000000000000000000000000000000000..a03d374740fb1fe5e68aeefc34f2dfaa2fa52eeb --- /dev/null +++ b/docs/Installation.md @@ -0,0 +1,12 @@ +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 +``` +NSHealthShareUsageDescription +Read and understand health data. +NSHealthUpdateUsageDescription +Share workout data with other apps. +``` diff --git a/docs/Permissions.md b/docs/Permissions.md new file mode 100644 index 0000000000000000000000000000000000000000..9756c54fe4ca39d06660b99cbc3577ba2982cb56 --- /dev/null +++ b/docs/Permissions.md @@ -0,0 +1,21 @@ +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 diff --git a/docs/authorizationStatusForType().md b/docs/authorizationStatusForType().md new file mode 100644 index 0000000000000000000000000000000000000000..8687b89b926df11f8348d2f46bbd5d2587454d30 --- /dev/null +++ b/docs/authorizationStatusForType().md @@ -0,0 +1,16 @@ +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). diff --git a/docs/getActiveEnergyBurned().md b/docs/getActiveEnergyBurned().md new file mode 100644 index 0000000000000000000000000000000000000000..17b31e1e5ff79319a9bad51dff9265105f9943c2 --- /dev/null +++ b/docs/getActiveEnergyBurned().md @@ -0,0 +1,18 @@ +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 diff --git a/docs/getBasalEnergyBurned().md b/docs/getBasalEnergyBurned().md new file mode 100644 index 0000000000000000000000000000000000000000..32f4521ab755eab24a3da44613fed4a23953a4c9 --- /dev/null +++ b/docs/getBasalEnergyBurned().md @@ -0,0 +1,16 @@ +```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) +}); +``` diff --git a/docs/getBiologicalSex().md b/docs/getBiologicalSex().md new file mode 100644 index 0000000000000000000000000000000000000000..358a8dc29d23f0758c9dcb57cb3554437cbcf70c --- /dev/null +++ b/docs/getBiologicalSex().md @@ -0,0 +1,23 @@ +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 diff --git a/docs/getBloodGlucoseSamples().md b/docs/getBloodGlucoseSamples().md new file mode 100644 index 0000000000000000000000000000000000000000..a89ebe8f24c77c8895ccd3b5112d8acda329d736 --- /dev/null +++ b/docs/getBloodGlucoseSamples().md @@ -0,0 +1,22 @@ +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) => { + if (err) { + return; + } + console.log(results) +}); +``` diff --git a/docs/getBloodPressureSamples().md b/docs/getBloodPressureSamples().md new file mode 100644 index 0000000000000000000000000000000000000000..29708a580a58eea854d58634a2abee18c8eb9e13 --- /dev/null +++ b/docs/getBloodPressureSamples().md @@ -0,0 +1,28 @@ +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) => { + 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 diff --git a/docs/getBodyFatPercentageSamples().md b/docs/getBodyFatPercentageSamples().md new file mode 100644 index 0000000000000000000000000000000000000000..8e3e872296efed719e906f9938445d2f0c0de77d --- /dev/null +++ b/docs/getBodyFatPercentageSamples().md @@ -0,0 +1,27 @@ +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) => { + 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' }, +] +``` diff --git a/docs/getBodyTemperatureSamples().md b/docs/getBodyTemperatureSamples().md new file mode 100644 index 0000000000000000000000000000000000000000..2d32c7d20736dced1cc225bb42ec6597aa2708a9 --- /dev/null +++ b/docs/getBodyTemperatureSamples().md @@ -0,0 +1,30 @@ +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) => { + 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 diff --git a/docs/getDailyDistanceCyclingSamples().md b/docs/getDailyDistanceCyclingSamples().md new file mode 100644 index 0000000000000000000000000000000000000000..43cedb3537c565b74733d4e29351443a0b22c120 --- /dev/null +++ b/docs/getDailyDistanceCyclingSamples().md @@ -0,0 +1,17 @@ +```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) => { + if (err) { + return; + } + console.log(results) +}); +``` diff --git a/docs/getDailyDistanceSwimmingSamples().md b/docs/getDailyDistanceSwimmingSamples().md new file mode 100644 index 0000000000000000000000000000000000000000..220451496853b24fc2fc473fc28aebb79489ad71 --- /dev/null +++ b/docs/getDailyDistanceSwimmingSamples().md @@ -0,0 +1,19 @@ +```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) => { + if (err) { + return; + } + console.log(results) +}); +``` diff --git a/docs/getDailyDistanceWalkingRunningSamples().md b/docs/getDailyDistanceWalkingRunningSamples().md new file mode 100644 index 0000000000000000000000000000000000000000..b6a288058d52619d9ba342b1a18d6e277db9e5dd --- /dev/null +++ b/docs/getDailyDistanceWalkingRunningSamples().md @@ -0,0 +1,17 @@ +```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) => { + if (err) { + return; + } + console.log(results) +}); +``` diff --git a/docs/getDailyFlightsClimbedSamples().md b/docs/getDailyFlightsClimbedSamples().md new file mode 100644 index 0000000000000000000000000000000000000000..ce9a6fd16476d0d49849ff1374af16865e9b958b --- /dev/null +++ b/docs/getDailyFlightsClimbedSamples().md @@ -0,0 +1,17 @@ +```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) => { + if (err) { + return; + } + console.log(results) +}); +``` diff --git a/docs/getDailyStepCountSamples().md b/docs/getDailyStepCountSamples().md new file mode 100644 index 0000000000000000000000000000000000000000..c8a0c6530cb533a3803615023ac109c8a5aa8314 --- /dev/null +++ b/docs/getDailyStepCountSamples().md @@ -0,0 +1,18 @@ +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) => { + if (this._handleHealthkitError(err, 'getDailyStepCountSamples')) { + return; + } + console.log(results) +}); +``` \ No newline at end of file diff --git a/docs/getDateOfBirth().md b/docs/getDateOfBirth().md new file mode 100644 index 0000000000000000000000000000000000000000..cb8d5933f19ae53dfa7877de1d67c965863e63e2 --- /dev/null +++ b/docs/getDateOfBirth().md @@ -0,0 +1,18 @@ +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 diff --git a/docs/getDistanceCycling().md b/docs/getDistanceCycling().md new file mode 100644 index 0000000000000000000000000000000000000000..ce5e1a602d9d5675c4f53d60a1b4fba05f7869d7 --- /dev/null +++ b/docs/getDistanceCycling().md @@ -0,0 +1,26 @@ +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 diff --git a/docs/getDistanceWalkingRunning().md b/docs/getDistanceWalkingRunning().md new file mode 100644 index 0000000000000000000000000000000000000000..308f7855e602df6e728e5e92de133138468738b2 --- /dev/null +++ b/docs/getDistanceWalkingRunning().md @@ -0,0 +1,27 @@ +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 diff --git a/docs/getFlightsClimbed().md b/docs/getFlightsClimbed().md new file mode 100644 index 0000000000000000000000000000000000000000..b9f4f4a359f3409246a240fc98fe1820ea53d8c9 --- /dev/null +++ b/docs/getFlightsClimbed().md @@ -0,0 +1,25 @@ +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 diff --git a/docs/getHeartRateSamples().md b/docs/getHeartRateSamples().md new file mode 100644 index 0000000000000000000000000000000000000000..05abf47cd5ae858fb31b7d116ed70c591e93126b --- /dev/null +++ b/docs/getHeartRateSamples().md @@ -0,0 +1,26 @@ +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) => { + 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 diff --git a/docs/getHeightSamples().md b/docs/getHeightSamples().md new file mode 100644 index 0000000000000000000000000000000000000000..ffb11c266a82b5c0f7cd9554cbd0fd224ff4584e --- /dev/null +++ b/docs/getHeightSamples().md @@ -0,0 +1,27 @@ +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) => { + 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 diff --git a/docs/getLatestBmi().md b/docs/getLatestBmi().md new file mode 100644 index 0000000000000000000000000000000000000000..2fd194071605909e0da58bc4cc70e0359e03f2d7 --- /dev/null +++ b/docs/getLatestBmi().md @@ -0,0 +1,20 @@ +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' +} +``` diff --git a/docs/getLatestBodyFatPercentage().md b/docs/getLatestBodyFatPercentage().md new file mode 100644 index 0000000000000000000000000000000000000000..7288387c59b0a626319569b4a7381ac379efcf34 --- /dev/null +++ b/docs/getLatestBodyFatPercentage().md @@ -0,0 +1,20 @@ +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 diff --git a/docs/getLatestHeight().md b/docs/getLatestHeight().md new file mode 100644 index 0000000000000000000000000000000000000000..3e317814a91d59e1d6fe4c0081951829d210542a --- /dev/null +++ b/docs/getLatestHeight().md @@ -0,0 +1,21 @@ +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 diff --git a/docs/getLatestLeanBodyMass().md b/docs/getLatestLeanBodyMass().md new file mode 100644 index 0000000000000000000000000000000000000000..b6a35b73a4429b747760886206467deac7e37b5b --- /dev/null +++ b/docs/getLatestLeanBodyMass().md @@ -0,0 +1,20 @@ +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 diff --git a/docs/getLatestWeight().md b/docs/getLatestWeight().md new file mode 100644 index 0000000000000000000000000000000000000000..4374328254f0a78f40f1558629ddb918770dde0b --- /dev/null +++ b/docs/getLatestWeight().md @@ -0,0 +1,27 @@ +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' +} +``` diff --git a/docs/getLeanBodyMassSamples().md b/docs/getLeanBodyMassSamples().md new file mode 100644 index 0000000000000000000000000000000000000000..93a0af87b066ad9222e84b35b8bcca365a9d5b04 --- /dev/null +++ b/docs/getLeanBodyMassSamples().md @@ -0,0 +1,28 @@ +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) => { + 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' }, +] +``` diff --git a/docs/getRespiratoryRateSamples().md b/docs/getRespiratoryRateSamples().md new file mode 100644 index 0000000000000000000000000000000000000000..67bbe86d62958454119def2466a6ed7036c17a6d --- /dev/null +++ b/docs/getRespiratoryRateSamples().md @@ -0,0 +1,22 @@ +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) => { + if (err) { + return; + } + console.log(results) +}); +``` diff --git a/docs/getSamples().md b/docs/getSamples().md new file mode 100644 index 0000000000000000000000000000000000000000..a4c6cc680f9c5ab362c31726d09453f992c39b37 --- /dev/null +++ b/docs/getSamples().md @@ -0,0 +1,52 @@ +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) => { + 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]] +} +``` diff --git a/docs/getSleepSamples().md b/docs/getSleepSamples().md new file mode 100644 index 0000000000000000000000000000000000000000..2fe71331293a7488fce37896c2566402ecd99dce --- /dev/null +++ b/docs/getSleepSamples().md @@ -0,0 +1,29 @@ +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) => { + if (err) { + return; + } + console.log(results). +}); +``` \ No newline at end of file diff --git a/docs/getStepCount().md b/docs/getStepCount().md new file mode 100644 index 0000000000000000000000000000000000000000..f24f81af12f977c60c59529f260b9b0d538891e5 --- /dev/null +++ b/docs/getStepCount().md @@ -0,0 +1,24 @@ +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 diff --git a/docs/getWeightSamples().md b/docs/getWeightSamples().md new file mode 100644 index 0000000000000000000000000000000000000000..e23e4b860b75fa1ef4fe5828aeabb411c85c0f91 --- /dev/null +++ b/docs/getWeightSamples().md @@ -0,0 +1,28 @@ +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) => { + 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 diff --git a/docs/initHealthKit().md b/docs/initHealthKit().md new file mode 100644 index 0000000000000000000000000000000000000000..dc0882e62d8bdc7da003f74ca21eaa84d7f6fa12 --- /dev/null +++ b/docs/initHealthKit().md @@ -0,0 +1,30 @@ +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 diff --git a/docs/initStepCountObserver().md b/docs/initStepCountObserver().md new file mode 100644 index 0000000000000000000000000000000000000000..118b014443c387a97cfd741a30aa2c1ad2a2bf18 --- /dev/null +++ b/docs/initStepCountObserver().md @@ -0,0 +1,64 @@ +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(); +} +``` diff --git a/docs/isAvailable().md b/docs/isAvailable().md new file mode 100644 index 0000000000000000000000000000000000000000..2cdb4f0b6e745f4368d77254803e0f22f1f23333 --- /dev/null +++ b/docs/isAvailable().md @@ -0,0 +1,13 @@ +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 diff --git a/docs/saveBmi().md b/docs/saveBmi().md new file mode 100644 index 0000000000000000000000000000000000000000..ce7ddb6a7fd41c4dc82f88c7e1bd6c96bc25fb00 --- /dev/null +++ b/docs/saveBmi().md @@ -0,0 +1,16 @@ +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 diff --git a/docs/saveBodyFatPercentage().md b/docs/saveBodyFatPercentage().md new file mode 100644 index 0000000000000000000000000000000000000000..e5f2bc39e36855e1c3a5e6673842f6cc2e5cbb30 --- /dev/null +++ b/docs/saveBodyFatPercentage().md @@ -0,0 +1,17 @@ +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 +}); +``` diff --git a/docs/saveHeight().md b/docs/saveHeight().md new file mode 100644 index 0000000000000000000000000000000000000000..1e8292b1a2ec6378220148d724f48c4ef9633353 --- /dev/null +++ b/docs/saveHeight().md @@ -0,0 +1,17 @@ +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 diff --git a/docs/saveLeanBodyMass().md b/docs/saveLeanBodyMass().md new file mode 100644 index 0000000000000000000000000000000000000000..44b7a317f29c0d905c21a6dfd2e949ee43e15c3d --- /dev/null +++ b/docs/saveLeanBodyMass().md @@ -0,0 +1,18 @@ +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 +}); +``` diff --git a/docs/saveMindfulSession().md b/docs/saveMindfulSession().md new file mode 100644 index 0000000000000000000000000000000000000000..4166912a732b5821c345d6abc91e46b5470a828d --- /dev/null +++ b/docs/saveMindfulSession().md @@ -0,0 +1,19 @@ +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 diff --git a/docs/saveSteps().md b/docs/saveSteps().md new file mode 100644 index 0000000000000000000000000000000000000000..a990b1248dc99bf2469cc203472cdf92d249461c --- /dev/null +++ b/docs/saveSteps().md @@ -0,0 +1,23 @@ +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 diff --git a/docs/saveWeight().md b/docs/saveWeight().md new file mode 100644 index 0000000000000000000000000000000000000000..d0c235b7b9151187e1db2d6fbc44c92253250f63 --- /dev/null +++ b/docs/saveWeight().md @@ -0,0 +1,18 @@ +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 diff --git a/docs/setObserver().md b/docs/setObserver().md new file mode 100644 index 0000000000000000000000000000000000000000..8f551613ca62dbf830ec32997dbeef3df68f3649 --- /dev/null +++ b/docs/setObserver().md @@ -0,0 +1,15 @@ +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. diff --git a/package.json b/package.json index 56ff849f4695f170d6692c47a7248da2b238fa82..f6027689d5033f2bd199e5920e7b0905923ecebb 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "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": {