使用@capgo/capacitor-health
Capacitor插件用于与Apple HealthKit和健康连接交互。
安装
bun add @capgo/capacitor-health
bunx cap sync
bunx cap sync 将插件的本机code复制到您的本机项目中。每次升级插件后,请再次运行它。
导入
import { Health } from '@capgo/capacitor-health';
API概览
| 方法 | 功能描述 |
|---|---|
isAvailable |
返回当前平台是否支持本地健康SDK。 |
requestAuthorization |
请求对提供的数据类型进行读写访问。 |
checkAuthorization |
检查提供的数据类型的授权状态,无需提示用户。 |
readSamples |
在指定时间范围内读取给定数据类型的样本。 |
saveSample |
将单个样本写入本地健康存储。 |
openHealthConnectSettings |
打开健康连接设置屏幕(仅Android)。在iOS上,这个方法什么也不做。 |
showPrivacyPolicy |
显示应用的健康连接隐私政策(仅Android)。在iOS上,这个方法什么也不做。 |
queryWorkouts |
从本地健康存储中查询运动会话。支持iOS(HealthKit)和Android(Health Connect)。 |
queryAggregated |
从本地健康存储中查询聚合健康数据。聚合数据以小时、天、周、月的时间桶中进行操作,如求和、平均值、最小值或最大值。这比为大范围日期获取单个样本更高效。 |
示例
isAvailable()
返回当前平台是否支持本地健康SDK。
import { Health } from '@capgo/capacitor-health';
const result = await Health.isAvailable();
console.log(result);
requestAuthorization()
请求读写访问所提供的数据类型。
import { Health } from '@capgo/capacitor-health';
const result = await Health.requestAuthorization({ read: ['steps'] });
console.log(result);
checkAuthorization()
检查授权状态所提供的数据类型,无需提示用户。
import { Health } from '@capgo/capacitor-health';
const result = await Health.checkAuthorization({ read: ['steps'] });
console.log(result);
readSamples()
在指定时间范围内读取给定数据类型的样本。
import { Health } from '@capgo/capacitor-health';
const result = await Health.readSamples({ dataType: 'steps' });
console.log(result);
saveSample()
将单个样本写入本机健康存储。
import { Health } from '@capgo/capacitor-health';
await Health.saveSample({
dataType: 'steps',
value: 1,
});
openHealthConnectSettings()
打开健康连接设置屏幕(仅Android)。在iOS中,这个方法什么也不做。
import { Health } from '@capgo/capacitor-health';
await Health.openHealthConnectSettings();
上表列出了9个核心方法。监听器和版本辅助,以及每个方法的完整契约,都在__CAPGO_KEEP_0__仓库中文档化。 GitHub参考文档.
完整参考
继续使用@capgo/capacitor-health
如果您正在使用 使用 @capgo/capacitor-health 为了规划原生插件工作,连接它与 @capgo/capacitor健康监控 查看 @capgo/capacitor-health 的实现细节 开始使用 查看 Getting Started 的实现细节 Capgo 插件目录 了解 Capgo 插件目录 中的产品工作流程 Capacitor 由 Capgo 提供的插件 查看 Capacitor 由 Capgo 提供的插件 的实现细节 添加或更新插件 为添加或更新插件的实现细节。