Ios 当应用程序未运行(已终止)时,HKObserverQuery是否可以接收通知?
我的要求是使用enableBackgroundDeliveryForType:方法注册任何一个健康数据,如步数、体重、心率等,用于后台传递。然后为要检查的相同健康数据创建观察者查询Ios 当应用程序未运行(已终止)时,HKObserverQuery是否可以接收通知?,ios,iphone,ios8,healthkit,hkobserverquery,Ios,Iphone,Ios8,Healthkit,Hkobserverquery,我的要求是使用enableBackgroundDeliveryForType:方法注册任何一个健康数据,如步数、体重、心率等,用于后台传递。然后为要检查的相同健康数据创建观察者查询 在这种情况下,如果我的应用程序被用户强制终止,并使用Health app更改了健康数据。到了这个阶段,是否可以将通知发送到我的应用程序 是否必须注册任何后台模式,才能通过HKObserverQuery获得健康数据修改通知 编辑1: - (void)requestAuthorizationToShareTypes:(N
- (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];
}
请帮帮我。谢谢。@tshortli,我已经在我的应用程序中应用了HKObserverQuery,然后也启用了后台交付。我希望在后台模式下启用位置服务,但为了启用HealthKit,我对位置服务没有要求。请添加您使用的代码片段。如果没有看到您已经尝试过的内容,就很难判断出什么可能是错误的。巴努-即使您的应用被用户明确杀死,您是否能够确定您的应用是否通过后台交付被唤醒?@PratikStephen请看我的答案:/cc BhanuPrakash