feat: 支持原生模块健康数据
This commit is contained in:
@@ -77,6 +77,44 @@ const HealthKitTest: React.FC = () => {
|
||||
}
|
||||
};
|
||||
|
||||
const handleCheckAuthorizationStatus = async () => {
|
||||
if (!state.isAvailable) {
|
||||
Alert.alert('错误', 'HealthKit不可用');
|
||||
return;
|
||||
}
|
||||
|
||||
setState(prev => ({ ...prev, loading: true, error: null }));
|
||||
|
||||
try {
|
||||
const result = await HealthKitManager.getAuthorizationStatus();
|
||||
|
||||
if (result.success) {
|
||||
const permissions = result.permissions;
|
||||
const sleepPermission = permissions['HKCategoryTypeIdentifierSleepAnalysis'];
|
||||
const authorized = sleepPermission === 'authorized';
|
||||
|
||||
setState(prev => ({ ...prev, isAuthorized: authorized, loading: false }));
|
||||
|
||||
const permissionDetails = Object.entries(permissions)
|
||||
.map(([key, value]) => `${key}: ${value}`)
|
||||
.join('\n');
|
||||
|
||||
Alert.alert(
|
||||
'权限状态',
|
||||
`当前权限状态:\n${permissionDetails}`,
|
||||
[{ text: '确定' }]
|
||||
);
|
||||
} else {
|
||||
setState(prev => ({ ...prev, loading: false }));
|
||||
Alert.alert('查询失败', '无法获取权限状态');
|
||||
}
|
||||
} catch (error) {
|
||||
const errorMessage = error instanceof Error ? error.message : String(error);
|
||||
setState(prev => ({ ...prev, loading: false, error: errorMessage }));
|
||||
Alert.alert('错误', `查询权限状态失败: ${errorMessage}`);
|
||||
}
|
||||
};
|
||||
|
||||
const handleGetSleepData = async () => {
|
||||
if (!state.isAuthorized) {
|
||||
Alert.alert('错误', '请先获取HealthKit授权');
|
||||
@@ -210,6 +248,16 @@ const HealthKitTest: React.FC = () => {
|
||||
</Text>
|
||||
</TouchableOpacity>
|
||||
|
||||
<TouchableOpacity
|
||||
style={[styles.button, !state.isAvailable && styles.buttonDisabled]}
|
||||
onPress={handleCheckAuthorizationStatus}
|
||||
disabled={!state.isAvailable || state.loading}
|
||||
>
|
||||
<Text style={styles.buttonText}>
|
||||
{state.loading ? '查询中...' : '检查权限状态'}
|
||||
</Text>
|
||||
</TouchableOpacity>
|
||||
|
||||
<TouchableOpacity
|
||||
style={[styles.button, (!state.isAuthorized || state.loading) && styles.buttonDisabled]}
|
||||
onPress={handleGetSleepData}
|
||||
|
||||
Reference in New Issue
Block a user