1

即使应用程序在后台,我也需要在服务器上上传步骤。我为 HKQuantityTypeIdentifierStepCount 添加了带有 enableBackgroundDeliveryForType 的 HKObserverQuery。

当从观察者收到更新通知时,它将查询一天的总步数(HKStatisticsCollectionQuery)。

到目前为止,它工作正常,执行查询过程后它停止,没有收到结果。苹果健康中有步骤,但结果不返回。

谁能知道它为什么停止或不返回结果?

[healthStore enableBackgroundDeliveryForType: [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount] frequency:HKUpdateFrequencyImmediate withCompletion:^(BOOL success, NSError *error) {
    NSLog(@"Delegate Observation registered error for steps=%@",error);
}];

HKobserverQuery 和 enableBackgroundDeliveryForType 都是从 didFinishLaunchingWithOptions 添加的。

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];

我创建了从苹果健康获取步骤并上传到服务器的自定义类,下面是获取步骤的方法代码。

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];

如果提供的信息不够,请询问任何人。我不知道我做错了什么。

4

0 回答 0