Does HKObserverQuery can receive notification when the application not running(Killed)?

混江龙づ霸主 提交于 2019-12-11 08:34:43

问题


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.

来源:https://stackoverflow.com/questions/29142367/does-hkobserverquery-can-receive-notification-when-the-application-not-runningk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!