如果没有运行,HealthKit后台交付可以启动应用程序吗?特别是在终止状态?
我们目前正在尝试让HealthKit在后台运行,以便在App关闭时向我们的服务器提供步骤数据.
出于实验目的,我们在XCode中创建了一个全新的iOS项目,启用了HealhtKit以及Compabilities中的所有后台模式.在那之后,我们几乎运行代码(见下文).
所以首先发生的是应用程序请求权限,我们授予.我们期待的是应用程序应该每小时向服务器提供步骤数据.但它没有这样做,似乎应用程序不能做任何事情,当它不活跃.
该应用程序仅在恢复或启动时提供数据,但在背景中根本不提供(软关闭/硬关闭)
appdelegate.m:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self setTypes];
return YES;
}
-(void) setTypes
{
self.healthStore = [[HKHealthStore alloc] init];
NSMutableSet* types = [[NSMutableSet alloc]init];
[types addObject:[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount]];
[self.healthStore requestAuthorizationToShareTypes: types
readTypes: types
completion:^(BOOL success, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
[self observeQuantityType];
[self enableBackgroundDeliveryForQuantityType];
});
}];
}
-(void)enableBackgroundDeliveryForQuantityType{
[self.healthStore enableBackgroundDeliveryForType: [HKQuantityType quantityTypeForIdentifier: HKQuantityTypeIdentifierStepCount] frequency:HKUpdateFrequencyImmediate withCompletion:^(BOOL success, NSError *error) {
}];
}
-(void) observeQuantityType{
HKSampleType *quantityType = [HKSampleType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];
HKObserverQuery *query =
[[HKObserverQuery alloc]
initWithSampleType:quantityType
predicate:nil
updateHandler:^(HKObserverQuery …Run Code Online (Sandbox Code Playgroud)