React Native Apple Healthkit 已经被弃用,因此不再推荐使用。Apple 在 WWDC 2020 中宣布了新的 Healthkit API 的可用性,并且在 iOS 14 及其后续版本中,旧的 Healthkit 框架已被新的 Healthkit SDK 替代。
如果你仍需要与 Healthkit 交互,你应该使用新的 Healthkit SDK,即 HealthKit 框架。以下是一个如何使用 Healthkit 获取步数的例子:
import React, { useEffect, useState } from 'react';
import { Text, View } from 'react-native';
import { HealthKit, HealthKitPermissions } from 'react-native-health';
 
const App = () => {
  const [steps, setSteps] = useState(0);
 
  useEffect(() => {
    (async () => {
      try {
        // 请求 Healthkit 的权限
        const permissions = {
          permissions: {
            read: [HealthKitPermissions.StepCount],
          },
        };
        await HealthKit.requestAuthorization(permissions);
 
        // 查询步数
        const endDate = new Date().toISOString();
        const startDate = new Date('2021-01-01').toISOString();
        const stepCount = await HealthKit.getStepCount(startDate, endDate);
        setSteps(stepCount);
      } catch (error) {
        console.error('Error fetching step count: ', error);
      }
    })();
  }, []);
 
  return (
    <View>
      <Text>Total Steps: {steps}</Text>
    </View>
  );
};
 
export default App;请注意,在实际应用中,你需要处理权限请求和错误处理,并确保遵守苹果的隐私政策。
如果你的应用需要与 Healthkit 交互,现在是迁移到新的 Healthkit SDK 的好时机。你可以参考官方文档或者社区库的最新示例来更新你的代码。