日常活动(TSDailyActivity)
该接口提供管理每日活动数据的方法,包括获取和设置运动目标、同步今天的活动数据、检索历史活动数据。每日活动跟踪包括步数、距离、卡路里和运动时长。
前提条件
- 设备已配对且连接正常
- 用户已授予应用访问健康数据的权限
- 设备固件版本支持日常活动功能
数据模型
TSDailyActivityGoals(每日运动目标模型)
| 属性名 | 类型 | 说明 |
|---|---|---|
stepsGoal | NSInteger | 每日步数目标。单位:步。范围:0–100,000(建议) |
caloriesGoal | NSInteger | 每日消耗卡路里目标。单位:千卡(kcal)。范围:50–3,000(建议) |
distanceGoal | NSInteger | 每日运动距离目标。单位:米(m)。范围:0–100,000(建议) |
activityDurationGoal | NSInteger | 每日活动时长目标。单位:分钟(min)。范围:0–1,440(24小时) |
exerciseDurationGoal | NSInteger | 每日运动时长目标。单位:分钟(min)。范围:0–1,440(24小时) |
exerciseTimesGoal | NSInteger | 每日运动次数目标。单位:次。范围:1–50(建议) |
activityTimesGoal | NSInteger | 每日活动次数目标。单位:次。范围:1–100(建议) |
TSDailyActivityItem(每日活动数据项)
| 属性名 | 类型 | 说明 |
|---|---|---|
steps | NSInteger | 当日总步数 |
calories | NSInteger | 消耗的总卡路里(小卡)。包括基础代谢率和活动导致的卡路里消耗 |
distance | NSInteger | 总距离(米)。根据步数和步幅估算计算得出 |
activityDuration | NSInteger | 总活动时长(秒)。包括所有超过最小活动阈值的运动时间 |
exercisesDuration | NSInteger | 运动时长(秒)。仅包括根据强度和持续时间判定为运动的活动 |
exercisesTimes | NSInteger | 运动次数。当天进行的运动会话总次数 |
activityTimes | NSInteger | 活动次数。包括所有超过最小活动阈值的身体活动 |
TSActivityDailyModel(每日聚合活动模型)
| 属性名 | 类型 | 说明 |
|---|---|---|
steps | NSInteger | 当日步数汇总。当日所有 TSDailyActivityItem 的步数总和 |
calories | NSInteger | 当日卡路里汇总(小卡)。当日所有条目的 calories 之和 |
distance | NSInteger | 当日距离汇总(米)。当日所有条目的 distance 之和 |
activityDuration | NSInteger | 当日活动时长汇总(秒)。当日所有条目的 activityDuration 之和 |
activityTimes | NSInteger | 当日活动次数汇总。当日所有条目的 activityTimes 之和 |
exercisesDuration | NSInteger | 当日运动时长汇总(秒)。当日所有条目的 exercisesDuration 之和 |
exercisesTimes | NSInteger | 当日运动次数汇总。当日所有条目的 exercisesTimes 之和 |
activityItems | NSArray<TSDailyActivityItem *> | 当天活动测量条目数组,按时间升序排列 |
TSDailyActivityReminder(每日运动目标提醒配置)
| 属性名 | 类型 | 说明 |
|---|---|---|
stepsReminderEnabled | BOOL | 步数目标提醒是否开启 |
caloriesReminderEnabled | BOOL | 卡路里目标提醒是否开启 |
distanceReminderEnabled | BOOL | 距离目标提醒是否开启 |
activityTimesReminderEnabled | BOOL | 活动次数目标提醒是否开启 |
activityDurationReminderEnabled | BOOL | 活动时长目标提醒是否开启 |
exerciseTimesReminderEnabled | BOOL | 运动次数目标提醒是否开启 |
exerciseDurationReminderEnabled | BOOL | 运动时长目标提醒是否开启 |
枚举与常量
TSDailyActivityType(每日活动类型枚举)
| 枚举值 | 值 | 说明 |
|---|---|---|
TSDailyActivityTypeStepCount | 1 | 步数 |
TSDailyActivityTypeExerciseDuration | 2 | 锻炼时长 |
TSDailyActivityTypeActivityCount | 3 | 活动次数 |
TSDailyActivityTypeActiveDuration | 4 | 活动时长 |
TSDailyActivityTypeDistance | 5 | 距离 |
TSDailyActivityTypeCalories | 6 | 卡路里 |
接口方法
获取所支持的每日活动类型
- (void)fetchSupportedDailyActivityTypesWithCompletion:(void(^)(NSArray<NSNumber *> *_Nullable activityTypes, NSError *_Nullable error))completion;
获取设备支持并显示的每日活动类型列表。返回的数组包含 TSDailyActivityType 枚举值,表示设备上可用的活动指标(例如:步数、锻炼时长等),用于配置主界面显示哪些活动环。
| 参数名 | 类型 | 说明 |
|---|---|---|
completion | void (^)(NSArray<NSNumber *> *_Nullable activityTypes, NSError *_Nullable error) | 返回支持的活动类型数组或错误的完成回调 |
代码示例:
id<TSDailyActivityInterface> dailyActivityInterface = [tsManager getInterfaceImpl:@protocol(TSDailyActivityInterface)];
[dailyActivityInterface fetchSupportedDailyActivityTypesWithCompletion:^(NSArray<NSNumber *> *activityTypes, NSError *error) {
if (error) {
TSLog(@"获取支持的活动类型失败: %@", error.localizedDescription);
return;
}
TSLog(@"设备支持的活动类型: %@", activityTypes);
for (NSNumber *typeNum in activityTypes) {
TSDailyActivityType type = (TSDailyActivityType)[typeNum integerValue];
TSLog(@"活动类型: %ld", (long)type);
}
}];
获取当前每日运动目标
- (void)fetchDailyExerciseGoalsWithCompletion:(void(^)(TSDailyActivityGoals *_Nullable goals, NSError *_Nullable error))completion;
从设备读取用户的每日运动目标,包括步数、卡路里、距离、活动/运动时长和运动次数。
| 参数名 | 类型 | 说明 |
|---|---|---|
completion | void (^)(TSDailyActivityGoals *_Nullable goals, NSError *_Nullable error) | 返回当前目标或错误的完成回调 |
代码示例:
id<TSDailyActivityInterface> dailyActivityInterface = [tsManager getInterfaceImpl:@protocol(TSDailyActivityInterface)];
[dailyActivityInterface fetchDailyExerciseGoalsWithCompletion:^(TSDailyActivityGoals *goals, NSError *error) {
if (error) {
TSLog(@"获取运动目标失败: %@", error.localizedDescription);
return;
}
TSLog(@"每日运动目标 - 步数: %ld, 卡路里: %ld, 距离: %ld",
(long)goals.stepsGoal,
(long)goals.caloriesGoal,
(long)goals.distanceGoal);
TSLog(@"活动时长: %ld分钟, 运动时长: %ld分钟, 运动次数: %ld",
(long)goals.activityDurationGoal,
(long)goals.exerciseDurationGoal,
(long)goals.exerciseTimesGoal);
}];
设置每日运动目标
- (void)pushDailyExerciseGoals:(TSDailyActivityGoals *)goalsModel
completion:(TSCompletionBlock)completion;
将用户的每日运动目标写入设备,包括步数、卡路里、距离、活动/运动时长与运动次数。
| 参数名 | 类型 | 说明 |
|---|---|---|
goalsModel | TSDailyActivityGoals * | 包含需设置的目标模型 |
completion | TSCompletionBlock | 指示操作是否成功及错误信息的完成回调 |
代码示例:
id<TSDailyActivityInterface> dailyActivityInterface = [tsManager getInterfaceImpl:@protocol(TSDailyActivityInterface)];
TSDailyActivityGoals *goals = [[TSDailyActivityGoals alloc] init];
goals.stepsGoal = 10000;
goals.caloriesGoal = 300;
goals.distanceGoal = 8000;
goals.activityDurationGoal = 60;
goals.exerciseDurationGoal = 30;
goals.exerciseTimesGoal = 3;
goals.activityTimesGoal = 10;
[dailyActivityInterface pushDailyExerciseGoals:goals completion:^(NSError *error) {
if (error) {
TSLog(@"设置运动目标失败: %@", error.localizedDescription);
return;
}
TSLog(@"运动目标已成功设置到设备");
}];
一次性获取每日运动目标与提醒配置
- (void)fetchDailyExerciseAllWithCompletion:(void(^)(TSDailyActivityGoals *_Nullable goals,
TSDailyActivityReminder *_Nullable reminder,
NSError *_Nullable error))completion;
为保证数据一致性,一次性读取每日运动目标(步数、卡路里、距离、活动/运动时长、次数)及其提醒开关。
| 参数名 | 类型 | 说明 |
|---|---|---|
completion | void (^)(TSDailyActivityGoals *_Nullable goals, TSDailyActivityReminder *_Nullable reminder, NSError *_Nullable error) | 返回目标模型、提醒模型与错误的完成回调 |
代码示例:
id<TSDailyActivityInterface> dailyActivityInterface = [tsManager getInterfaceImpl:@protocol(TSDailyActivityInterface)];
[dailyActivityInterface fetchDailyExerciseAllWithCompletion:^(TSDailyActivityGoals *goals, TSDailyActivityReminder *reminder, NSError *error) {
if (error) {
TSLog(@"获取运动目标和提醒配置失败: %@", error.localizedDescription);
return;
}
TSLog(@"步数目标: %ld, 提醒开启: %@",
(long)goals.stepsGoal,
reminder.stepsReminderEnabled ? @"是" : @"否");
TSLog(@"卡路里目标: %ld, 提醒开启: %@",
(long)goals.caloriesGoal,
reminder.caloriesReminderEnabled ? @"是" : @"否");
}];
原子性写入每日运动目标与提醒配置
- (void)pushDailyExerciseGoals:(TSDailyActivityGoals *)goalsModel
reminder:(TSDailyActivityReminder *)reminder
completion:(TSCompletionBlock)completion;
将目标与提醒聚合为一次底层写入,避免出现中间不一致状态。
| 参数名 | 类型 | 说明 |
|---|---|---|
goalsModel | TSDailyActivityGoals * | 需要设置的目标模型 |
reminder | TSDailyActivityReminder * | 需要设置的提醒开关模型 |
completion | TSCompletionBlock | 指示操作是否成功及错误信息的完成回调 |
代码示例:
id<TSDailyActivityInterface> dailyActivityInterface = [tsManager getInterfaceImpl:@protocol(TSDailyActivityInterface)];
TSDailyActivityGoals *goals = [[TSDailyActivityGoals alloc] init];
goals.stepsGoal = 8000;
goals.caloriesGoal = 250;
goals.distanceGoal = 6000;
goals.activityDurationGoal = 45;
goals.exerciseDurationGoal = 25;
goals.exerciseTimesGoal = 2;
goals.activityTimesGoal = 8;
TSDailyActivityReminder *reminder = [[TSDailyActivityReminder alloc] init];
reminder.stepsReminderEnabled = YES;
reminder.caloriesReminderEnabled = YES;
reminder.distanceReminderEnabled = YES;
reminder.activityTimesReminderEnabled = YES;
reminder.activityDurationReminderEnabled = YES;
reminder.exerciseTimesReminderEnabled = YES;
reminder.exerciseDurationReminderEnabled = YES;
[dailyActivityInterface pushDailyExerciseGoals:goals reminder:reminder completion:^(NSError *error) {
if (error) {
TSLog(@"设置目标和提醒失败: %@", error.localizedDescription);
return;
}
TSLog(@"目标和提醒已成功设置到设备");
}];
获取当前每日运动目标提醒配置
- (void)fetchDailyExerciseReminderConfigWithCompletion:(void(^)(TSDailyActivityReminder *_Nullable reminder, NSError *_Nullable error))completion;
获取目标对应的提醒开关:步数、卡路里、距离、活动时长、运动时长与运动次数。
| 参数名 | 类型 | 说明 |
|---|---|---|
completion | void (^)(TSDailyActivityReminder *_Nullable reminder, NSError *_Nullable error) | 返回提醒开关或错误的完成回调 |
代码示例:
id<TSDailyActivityInterface> dailyActivityInterface = [tsManager getInterfaceImpl:@protocol(TSDailyActivityInterface)];
[dailyActivityInterface fetchDailyExerciseReminderConfigWithCompletion:^(TSDailyActivityReminder *reminder, NSError *error) {
if (error) {
TSLog(@"获取提醒配置失败: %@", error.localizedDescription);
return;
}
TSLog(@"步数提醒: %@", reminder.stepsReminderEnabled ? @"开启" : @"关闭");
TSLog(@"卡路里提醒: %@", reminder.caloriesReminderEnabled ? @"开启" : @"关闭");
TSLog(@"距离提醒: %@", reminder.distanceReminderEnabled ? @"开启" : @"关闭");
TSLog(@"活动时长提醒: %@", reminder.activityDurationReminderEnabled ? @"开启" : @"关闭");
TSLog(@"运动时长提醒: %@", reminder.exerciseDurationReminderEnabled ? @"开启" : @"关闭");
TSLog(@"运动次数提���: %@", reminder.exerciseTimesReminderEnabled ? @"开启" : @"关闭");
}];
设置每日运动目标提醒配置
- (void)pushDailyExerciseReminder:(TSDailyActivityReminder *)reminder
completion:(TSCompletionBlock)completion;
写入步数、卡路里、距离、活动/运动时长与运动次数的提醒开关。
| 参数名 | 类型 | 说明 |
|---|---|---|
reminder | TSDailyActivityReminder * | 需要设置的提醒开关模型 |
completion | TSCompletionBlock | 指示操作是否成功及错误信息的完成回调 |
代码示例:
id<TSDailyActivityInterface> dailyActivityInterface = [tsManager getInterfaceImpl:@protocol(TSDailyActivityInterface)];
TSDailyActivityReminder *reminder = [[TSDailyActivityReminder alloc] init];
reminder.stepsReminderEnabled = YES;
reminder.caloriesReminderEnabled = NO;
reminder.distanceReminderEnabled = YES;
reminder.activityTimesReminderEnabled = YES;
reminder.activityDurationReminderEnabled = YES;
reminder.exerciseTimesReminderEnabled = YES;
reminder.exerciseDurationReminderEnabled = NO;
[dailyActivityInterface pushDailyExerciseReminder:reminder completion:^(NSError *error) {
if (error) {
TSLog(@"设置提醒配置失败: %@", error.localizedDescription);
return;
}
TSLog(@"��醒配置已成功设置到设备");
}];
同步今天的每日运动数据
- (void)syncTodayDailyExerciseDataCompletion:(void (^)(TSActivityDailyModel *_Nullable todayActivity, NSError *_Nullable error))completion;
从设备获取当天的活动数据,包括步数、行走距离、消耗的卡路里、活动时长、运动会话信息等,提供用户当天进度的快照。
| 参数名 | 类型 | 说明 |
|---|---|---|
completion | void (^)(TSActivityDailyModel *_Nullable todayActivity, NSError *_Nullable error) | 包含今天的运动数据或错误的完成回调 |
代码示例:
id<TSDailyActivityInterface> dailyActivityInterface = [tsManager getInterfaceImpl:@protocol(TSDailyActivityInterface)];
[dailyActivityInterface syncTodayDailyExerciseDataCompletion:^(TSActivityDailyModel *todayActivity, NSError *error) {
if (error) {
TSLog(@"同步今日数据失败: %@", error.localizedDescription);
return;
}
TSLog(@"今日活动数据:");
TSLog(@"步数: %ld", (long)todayActivity.steps);
TSLog(@"卡路里: %ld", (long)todayActivity.calories);
TSLog(@"距离: %ld米", (long)todayActivity.distance);
TSLog(@"活动时长: %ld秒", (long)todayActivity.activityDuration);
TSLog(@"运动时长: %ld秒", (long)todayActivity.exercisesDuration);
TSLog(@"运动次数: %ld", (long)todayActivity.exercisesTimes);
TSLog(@"详细条目数: %lu", (unsigned long)todayActivity.activityItems.count);
}];
同步指定时间范围内的原始每日活动数据
- (void)syncRawDataFromStartTime:(NSTimeInterval)startTime
endTime:(NSTimeInterval)endTime
completion:(nonnull void (^)(NSArray<TSDailyActivityItem *> *_Nullable activityItems, NSError *_Nullable error))completion;
检索指定时间范围内的历史每日活动数据。每天的数据表示为单独的 TSDailyActivityItem 对象。
| 参数名 | 类型 | 说明 |
|---|---|---|
startTime | NSTimeInterval | 数据同步的开始时间(1970年以来的秒数时间戳) |
endTime | NSTimeInterval | 数据同步的结束时间(1970年以来的秒数时间戳) |
completion | void (^)(NSArray<TSDailyActivityItem *> *_Nullable activityItems, NSError *_Nullable error) | 包含同步的原始每日活动测量条目或错误的完成回调 |
代码示例:
id<TSDailyActivityInterface> dailyActivityInterface = [tsManager getInterfaceImpl:@protocol(TSDailyActivityInterface)];
// 获取最近7天的原始数据
NSDate *now = [NSDate date];
NSTimeInterval endTime = [now timeIntervalSince1970];
NSTimeInterval startTime = endTime - (7 * 24 * 3600); // 7天前
[dailyActivityInterface syncRawDataFromStartTime:startTime
endTime:endTime
completion:^(NSArray<TSDailyActivityItem *> *activityItems, NSError *error) {
if (error) {
TSLog(@"同步原始数据失败: %@", error.localizedDescription);
return;
}
TSLog(@"同步了 %lu 条原始数据记录", (unsigned long)activityItems.count);
for (TSDailyActivityItem *item in activityItems) {
TSLog(@"时间戳: %.0f, 步数: %ld, 卡路里: %ld, 距离: %ld",
item.startTime,
(long)item.steps,
(long)item.calories,
(long)item.distance);
}
}];
同步指定开始时间至今的原始每日活动数据
- (void)syncRawDataFromStartTime:(NSTimeInterval)startTime
completion:(nonnull void (^)(NSArray<TSDailyActivityItem *> *_Nullable activityItems, NSError *_Nullable error))completion;
从指定开始时间到当前时间检索历史每日活动数据。这是便于获取最近的活动历史而无需指定结束日期的便捷方法。
| 参数名 | 类型 | 说明 |
|---|---|---|
startTime | NSTimeInterval | 数据同步的开始时间(1970年以来的秒数时间戳) |
completion | void (^)(NSArray<TSDailyActivityItem *> *_Nullable activityItems, NSError *_Nullable error) | 包含同步的原始每日活动测量条目或错误的完成回调 |
代码示例:
id<TSDailyActivityInterface> dailyActivityInterface = [tsManager getInterfaceImpl:@protocol(TSDailyActivityInterface)];
// 获取最近30天的原始数据
NSDate *startDate = [[NSDate date] dateByAddingTimeInterval:-(30 * 24 * 3600)];
NSTimeInterval startTime = [startDate timeIntervalSince1970];
[dailyActivityInterface syncRawDataFromStartTime:startTime
completion:^(NSArray<TSDailyActivityItem *> *activityItems, NSError *error) {
if (error) {
TSLog(@"同步原始数据失败: %@", error.localizedDescription);
return;
}
TSLog(@"同步了 %lu 条原始数据记录", (unsigned long)activityItems.count);
}];
同步指定时间范围内的每日活动数据
- (void)syncDailyDataFromStartTime:(NSTimeInterval)startTime
endTime:(NSTimeInterval)endTime
completion:(nonnull void (^)(NSArray<TSActivityDailyModel *> *_Nullable dailyModels, NSError *_Nullable error))completion;
此方法同步指定时间范围内的每日聚合活动数据。时间参数将自动规范化为日期边界(00:00:00 到 23:59:59),数据按时间升序返回,每个元素代表一天。
| 参数名 | 类型 | 说明 |
|---|---|---|
startTime | NSTimeInterval | 数据同步的开始时间(1970年以来的秒数时间戳)。将自动规范化为指定日期的 00:00:00。必须早于结束时间 |
endTime | NSTimeInterval | 数据同步的结束时间(1970年以来的秒数时间戳)。将自动规范化为指定日期的 23:59:59。必须晚于开始时间且不能在将来 |
completion | void (^)(NSArray<TSActivityDailyModel *> *_Nullable dailyModels, NSError *_Nullable error) | 完成回调,返回同步的每日活动模型数组或错误。每个 TSActivityDailyModel 代表一天的数据集合 |
代码示例:
id<TSDailyActivityInterface> dailyActivityInterface = [tsManager getInterfaceImpl:@protocol(TSDailyActivityInterface)];
// 获取最近7天的每日聚合数据
NSDate *now = [NSDate date];
NSTimeInterval endTime = [now timeIntervalSince1970];
NSTimeInterval startTime = endTime - (7 * 24 * 3600); // 7天前
[dailyActivityInterface syncDailyDataFromStartTime:startTime
endTime:endTime
completion:^(NSArray<TSActivityDailyModel *> *dailyModels, NSError *error) {
if (error) {
TSLog(@"同步每日数据失败: %@", error.localizedDescription);
return;
}
TSLog(@"同步了 %lu 天的每日数据", (unsigned long)dailyModels.count);
for (TSActivityDailyModel *dailyModel in dailyModels) {
TSLog(@"日期: %.0f-%.0f, 步数: %ld, 卡路里: %ld, 距离: %ld",
dailyModel.startTime,
dailyModel.endTime,
(long)dailyModel.steps,
(long)dailyModel.calories,
(long)dailyModel.distance);
}
}];
同步指定开始时间至今的每日活动数据
- (void)syncDailyDataFromStartTime:(NSTimeInterval)startTime
completion:(nonnull void (^)(NSArray<TSActivityDailyModel *> *_Nullable dailyModels, NSError *_Nullable error))completion;
此方法从开始时间到当前时间同步每日聚合活动数据。它是 syncDailyDataFromStartTime:endTime:completion: 的便捷包装,自动将结束时间设置为当前时间。
| 参数名 | 类型 | 说明 |
|---|---|---|
startTime | NSTimeInterval | 数据同步的开始时间(1970年以来的秒数时间戳)。将自动规范化为指定日期的 00:00:00 |
completion | void (^)(NSArray<TSActivityDailyModel *> *_Nullable dailyModels, NSError *_Nullable error) | 完成回调,返回同步的每日活动模型数组或错误。每个 TSActivityDailyModel 代表一天的数据集合 |
代码示例:
id<TSDailyActivityInterface> dailyActivityInterface = [tsManager getInterfaceImpl:@protocol(TSDailyActivityInterface)];
// 获取最近30天的每日聚合数据
NSDate *startDate = [[NSDate date] dateByAddingTimeInterval:-(30 * 24 * 3600)];
NSTimeInterval startTime = [startDate timeIntervalSince1970];
[dailyActivityInterface syncDailyDataFromStartTime:startTime
completion:^(NSArray<TSActivityDailyModel