1

My requirement is to register for any one of the Health data like steps, weight, heart rate, etc., for background delivery using enableBackgroundDeliveryForType: method. And then create a Observer query for the same Health data to check.

  1. In this scenario, if my application is killed by the user forcely and did changes to Health data using Health app. By this stage is it possible to get the notification to My application?

  2. Is it mandatory to register for any of the background modes, to get notified for health data modifications through HKObserverQuery?

EDIT 1:

- (void)requestAuthorizationToShareTypes:(NSSet *)typesToShare readTypes:(NSSet *)typesToRead completion:(void (^)(BOOL, NSError *))completion
{
    if ([HKHealthStore isHealthDataAvailable]) {

        [self.healthStore requestAuthorizationToShareTypes:typesToShare readTypes:typesToRead completion:^(BOOL success, NSError *error) {

            if(success)
            {
                self.authorizationSuccess = YES;
                completion(YES, nil);
            }
            else
            {
                [MyUtilities showAlertWithTitle:@"Authorization fail!" message:@"You didn't allow to access Health data."];
                completion(NO, error);
                return;
            }
        }];
    }
    else
    {
        [MyUtilities showAlertWithTitle:@"Sorry!" message:@"Your device does not support Health data."];
        NSDictionary *errorDictionary = @{ NSLocalizedDescriptionKey : @"Your device doesn't support Health Kit."};
        NSError *error = [[NSError alloc] initWithDomain:@"" code:2 userInfo:errorDictionary];
        completion(NO, error);
        return;
    }
}
- (void)authorizeConsumeHealth:(void (^)(BOOL, NSError *))completion
{
    NSSet *readObjectTypes  = [NSSet setWithObjects:
                               [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryCaffeine],
                               [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryCalcium],
                               [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryChloride],
                               [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryIron],
                               [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount],
                               [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierBloodPressureSystolic],
                               [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierBloodPressureDiastolic],                               [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount],
                               nil];

    [self requestAuthorizationToShareTypes:nil readTypes:readObjectTypes completion:^(BOOL success, NSError *error) {
        if(completion)
        {
            completion(success, error);
            [self observeStepCountChanges];
        }

    }];

}

- (void)observeStepCountChanges
{
    HKSampleType *sampleType = [HKSampleType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];

    [self.healthStore enableBackgroundDeliveryForType:sampleType frequency:HKUpdateFrequencyImmediate withCompletion:^(BOOL success, NSError *error) {}];


    HKObserverQuery *query = [[HKObserverQuery alloc] initWithSampleType:sampleType predicate:nil updateHandler:^(HKObserverQuery *query, HKObserverQueryCompletionHandler completionHandler, NSError *error) {
        if(error)
        {

            NSLog(@"Steps count observer completion block. Error: %@", error);
        }
        else
        {
            NSLog(@"Steps count observer completion block");

        }
    }];
    [self.healthStore executeQuery:query];


}

Please help me. Thanks.

Bhanu Prakash
  • 1,493
  • 8
  • 20
  • @tshortli, I have applied HKObserverQuery in my application and then enabled background delivery too. – Bhanu Prakash Mar 24 '15 at 12:03
  • Wantedly enabled location services in background mode though I don't have a requirement for location services, for the sake of HealthKit I enabled. – Bhanu Prakash Mar 24 '15 at 12:51
  • Please add the snippet of code you've used. It's hard to say what might be wrong without seeing what you have tried already. – Allan Mar 24 '15 at 17:59
  • Bhanu - were you able to figure out if your app get woken up via background delivery even if it is explicitly killed by the user? – Pratik Stephen Nov 22 '15 at 07:37
  • @PratikStephen - I did not worked on it after that words. I did this work as a part of R&D. – Bhanu Prakash Nov 23 '15 at 06:57
  • 1
    @PratikStephen look at my answer here: http://stackoverflow.com/a/35073904/1677480. /cc BhanuPrakash – damirstuhec Jan 28 '16 at 23:20

0 Answers0