Может ли HKObserverQuery получать уведомления, когда приложение не запущено (Killed)?

Мое требование состоит в том, чтобы зарегистрироваться для получения любых данных о состоянии здоровья, таких как шаги, вес, частота сердечных сокращений и т. Д., Для фоновой доставки с использованием метода enableBackgroundDeliveryForType:. А затем создайте запрос Observer для тех же данных о работоспособности, чтобы проверить.

  1. В этом случае, если мое приложение было принудительно убито пользователем и внесены изменения в данные о работоспособности с помощью приложения работоспособности. На этом этапе возможно ли получить уведомление в Мою заявку?

  2. Обязательно ли регистрироваться для любого из фоновых режимов, чтобы получать уведомления об изменениях данных о состоянии здоровья через HKObserverQuery?

РЕДАКТИРОВАТЬ 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];


}

Пожалуйста, помогите мне. Благодарю.

0 ответов

Другие вопросы по тегам