我如何使用 CYCHealthKitHandler 将 activity 定义为 "Indoor Cycling"

How I define my activity as "Indoor Cycling" using CYCHealthKitHandler

问题是我正在从我的应用发送 "Cycling" activity 到 Apple Health,我希望它显示为 "Indoor Cycling" 但所有活动都发送到 apple health activity 被标记为 "Outdoor activity",是否有任何 属性 将它们定义为我的代码中的 "Indoor Cycling" 活动?

我进行了研究,但只有一个 activity 可以定义循环,如您所见,我在下面的代码中使用了: workoutWithActivityType:HKWorkoutActivityTypeCycling

这是我的活动在 Apple Activity 应用程序中的视图

- (void)saveWorkoutFromStartDate:(NSDate *)startDate endDate:(NSDate *)endDate duration:(double)duration miles:(double)miles kilocalories:(double)calories completion:(void (^)(BOOL success, NSError *error))completion {

    HKQuantity *milesQuantity = [HKQuantity quantityWithUnit:[HKUnit mileUnit] doubleValue:miles];
    HKQuantity *caloriesQuantity = [HKQuantity quantityWithUnit:[HKUnit kilocalorieUnit] doubleValue:calories];

    HKWorkout *workout = [HKWorkout workoutWithActivityType:HKWorkoutActivityTypeCycling startDate:startDate endDate:endDate duration:duration totalEnergyBurned:caloriesQuantity totalDistance:milesQuantity metadata:nil];
    [self.healthStore saveObject:workout withCompletion:^(BOOL success, NSError *error) {

        if (success) {
            completion(success, nil);
        }
        else {
            DLog(@"%@", error);
            completion(success, error);
        }
    }];
}

尝试在实例化锻炼时将值为 YES 的元数据室内锻炼密钥添加到元数据字典中:

HKWorkout *workout = [HKWorkout workoutWithActivityType:HKWorkoutActivityTypeCycling startDate:startDate endDate:endDate duration:duration totalEnergyBurned:caloriesQuantity totalDistance:milesQuantity metadata:@{HKMetadataKeyIndoorWorkout : @(YES)}];

https://developer.apple.com/documentation/healthkit/hkmetadatakeyindoorworkout?language=objc

这是在您的 HKWorkoutConfigurationlocationType 实例 属性 上设置的。

https://developer.apple.com/documentation/healthkit/hkworkoutconfiguration/1649491-locationtype?changes=_4&language=objc

使用HKMetadataKeyIndoorWorkout

HKMetadataKeyIndoorWorkout 对我有用,例如:

var isIndoorWorkout = true 
var metadata: Dictionary<String,Any> {
    var _metadata:Dictionary<String,Any> = [:]
    _metadata[HKMetadataKeySyncIdentifier] = "Your unique workout Identifier goes here"
    _metadata[HKMetadataKeySyncVersion] = yourDateCompletedAt.timeIntervalSince1970 // as an example for versioning
    _metadata[HKMetadataKeyIndoorWorkout] = isIndoorWorkout // <-- HERE!
    return _metadata
}

来源:HKMetadataKeyIndoorWorkout