即使app在后台,我也需要在服务器上上传步骤。我已经为HKQuantityTypeIdentifierStepCount添加了带有enableBackgroundDeliveryForType的HKObserverQuery。
当从观察者收到更新通知时,它将查询一天的总步骤(HKStatisticsCollectionQuery)。
到目前为止它工作正常,在执行查询过程后它停止了,结果没有收到。在apple健康状态中存在但是结果没有返回。
有谁知道为什么它停止或不返回结果?
[healthStore enableBackgroundDeliveryForType: [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount] frequency:HKUpdateFrequencyImmediate withCompletion:^(BOOL success, NSError *error) {
NSLog(@"Delegate Observation registered error for steps=%@",error);
}];
从didFinishLaunchingWithOptions添加的HKobserverQuery和enableBackgroundDeliveryForType。
HKSampleType *quantityType = [HKSampleType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];
HKObserverQuery *query =
[[HKObserverQuery alloc]
initWithSampleType:quantityType
predicate:nil
updateHandler:^(HKObserverQuery *query,
HKObserverQueryCompletionHandler completionHandler,
NSError *error) {
CustomClass *customVC = [[CustomClass alloc] init];
[customVC fetchSteps:completionHandler];
}];
[self.healthApp.healthStore executeQuery:query];
我创建了自定义类,从apple运行中获取步骤并上传到服务器,下面是获取步骤的方法代码。
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *interval = [[NSDateComponents alloc] init];
interval.day = 1;
// Set the anchor date to Monday at 3:00 a.m.
NSDateComponents *anchorComponents =
[calendar components:NSCalendarUnitDay | NSCalendarUnitMonth |
NSCalendarUnitYear fromDate:[NSDate date]];
anchorComponents.day -= 1;
anchorComponents.hour = 0;
NSDate *anchorDate = [calendar dateFromComponents:anchorComponents];
NSLog(@"Anchor date::::: %@",anchorDate);
HKQuantityType *quantityType =
[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];
// Create the query
HKStatisticsCollectionQuery *query =
[[HKStatisticsCollectionQuery alloc]
initWithQuantityType:quantityType
quantitySamplePredicate:nil
options:HKStatisticsOptionCumulativeSum
anchorDate:anchorDate
intervalComponents:interval];
// Set the results handler
query.initialResultsHandler =
^(HKStatisticsCollectionQuery *query, HKStatisticsCollection *results, NSError *error) {
if (error) {
NSLog(@"An error occurred while calculating the statistics:::: %@",error);
return ;
}
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
NSDateFormatter *formate=[[NSDateFormatter alloc] init];
[formate setTimeZone:[NSTimeZone localTimeZone]];
[formate setDateFormat:@"yyyy-MM-dd"];
NSDate *startDate;
NSDate *endDate;
NSLog(@"Current date::: %@",startdt);
NSString *dateStr = [formate stringFromDate:startdt];
startDate = [formate dateFromString:dateStr];
dateStr = [NSString stringWithFormat:@"%@ 23:59:59",dateStr];
[formate setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
endDate = [formate dateFromString:dateStr];
[components setDay:-1];
startDate = [cal dateByAddingComponents:components toDate:startDate options:0];
NSDateFormatter *form=[[NSDateFormatter alloc] init];
[form setDateFormat:@"dd-MM-yyyy HH:mm:ss"];
// Gether all data
[results
enumerateStatisticsFromDate:startDate
toDate:endDate
withBlock:^(HKStatistics *result, BOOL *stop) {
HKQuantity *quantity = result.sumQuantity;
if (quantity) {
double value = [quantity doubleValueForUnit:hkUnit];
NSString *strValue = [NSString stringWithFormat:@"%f",value];
NSDate *date = result.startDate;
[self uploadSteps:date steps:strValue];
}
}];
};
[self.healthStore executeQuery:query];
如果给出的信息不够,请询问。我不知道自己做错了什么。