【发布时间】:2015-02-11 15:34:14
【问题描述】:
您好,我正在尝试设置启用了后台交付的健康商店观察者。我的问题是屏幕锁定时它不会提供任何东西。我已经简化了我的代码来解决这个问题:) 我的 plist 中有 HealthKit,并且我接受了 healthStore 类型的步数。 当应用程序打开并且屏幕未锁定时,一切都很好。但是当屏幕被锁定时,我没有得到任何观察。 出于测试目的,频率设置为立即。
我的代码如下
- (void)setupHealthStore{
if ([HKHealthStore isHealthDataAvailable])
{
NSSet *readDataTypes = [self dataTypesToRead];
self.healthStore = [[HKHealthStore alloc]init];
[self.healthStore requestAuthorizationToShareTypes:nil readTypes:readDataTypes completion:^(BOOL success, NSError *error)
{
if (success)
{
HKQuantityType *quantityType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];
[self.healthStore enableBackgroundDeliveryForType:quantityType frequency:HKUpdateFrequencyImmediate withCompletion:^(BOOL success, NSError *error)
{
if (success)
{
[self setupObserver];
}
}];
}
}];
}
}
上述方法在AppDelegate didfinishLaunchWithOptions中调用
下一个方法是
- (void)setupObserver{
HKQuantityType *quantityType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];
HKObserverQuery *query = [[HKObserverQuery alloc]initWithSampleType:quantityType predicate:nil updateHandler:^(HKObserverQuery *query, HKObserverQueryCompletionHandler completionHandler, NSError *error)
{
if (!error)
{
[self alarm];
if (completionHandler)
{
NSLog(@"Completed");
completionHandler();
}
}
else
{
if (completionHandler)
{
completionHandler();
}
}
}];
[self.healthStore executeQuery:query];
}
当我打开应用程序时,它会立即返回观察结果。
【问题讨论】:
标签: ios objective-c background-process healthkit hkhealthstore