diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.h b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.h index 3a859b0354f78ed7d5d089bbfc61711d5e1c4a49..a396875d511f446bce74c1d3c9c0ee19625de78b 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.h +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.h @@ -18,6 +18,8 @@ - (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_getDistanceCyclingOnDay:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; - (void)fitness_getFlightsClimbedOnDay:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.m b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.m index f885df4b2e15545fb83922c1c5eff7bee0c27477..3b5ae887b7d0e3d6e01e549fe7214771dbafa502 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.m @@ -10,8 +10,12 @@ #import "RCTAppleHealthKit+Queries.h" #import "RCTAppleHealthKit+Utils.h" +#import "RCTBridge.h" +#import "RCTEventDispatcher.h" + @implementation RCTAppleHealthKit (Methods_Fitness) + //- (void)fitness_getStepCountForToday:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback //{ // HKQuantityType *stepCountType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount]; @@ -156,6 +160,38 @@ +- (void)fitness_initializeStepEventObserver:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback +{ + HKSampleType *sampleType = + [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount]; + + HKObserverQuery *query = + [[HKObserverQuery alloc] + initWithSampleType:sampleType + predicate:nil + updateHandler:^(HKObserverQuery *query, + HKObserverQueryCompletionHandler completionHandler, + 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)]); + return; + } + + [self.bridge.eventDispatcher sendAppEventWithName:@"change:steps" + body:@{@"name": @"change:steps"}]; + + // If you have subscribed for background updates you must call the completion handler here. + // completionHandler(); + + }]; + + [self.healthStore executeQuery:query]; +} + + - (void)fitness_getDistanceWalkingRunningOnDay:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback { diff --git a/RCTAppleHealthKit/RCTAppleHealthKit.m b/RCTAppleHealthKit/RCTAppleHealthKit.m index f016e75b7f0045f07e3f40603024b1e8b498131c..036881b55656c64aea88b668cb913e0944653707 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit.m @@ -17,8 +17,14 @@ #import "RCTAppleHealthKit+Methods_Vitals.h" #import "RCTAppleHealthKit+Methods_Results.h" +#import "RCTBridge.h" +#import "RCTEventDispatcher.h" + @implementation RCTAppleHealthKit +@synthesize bridge = _bridge; + + RCT_EXPORT_MODULE(); @@ -32,6 +38,11 @@ RCT_EXPORT_METHOD(initHealthKit:(NSDictionary *)input callback:(RCTResponseSende [self initializeHealthKit:input callback:callback]; } +RCT_EXPORT_METHOD(initStepCountObserver:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback) +{ + [self fitness_initializeStepEventObserver:input callback:callback]; +} + RCT_EXPORT_METHOD(getBiologicalSex:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback) { [self characteristic_getBiologicalSex:input callback:callback]; @@ -181,7 +192,7 @@ RCT_EXPORT_METHOD(getInfo:(NSDictionary *)input callback:(RCTResponseSenderBlock - (void)initializeHealthKit:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback { self.healthStore = [[HKHealthStore alloc] init]; - + if ([HKHealthStore isHealthDataAvailable]) { NSSet *writeDataTypes; NSSet *readDataTypes; @@ -193,7 +204,7 @@ RCT_EXPORT_METHOD(getInfo:(NSDictionary *)input callback:(RCTResponseSenderBlock NSArray* writePermsArray = [permissions objectForKey:@"write"]; NSSet* readPerms = [self getReadPermsFromOptions:readPermsArray]; NSSet* writePerms = [self getWritePermsFromOptions:writePermsArray]; - + if(readPerms != nil) { readDataTypes = readPerms; }