react-native - 授予 Apple Health Kit 许可后转到下一页
问题描述
我正在我的 React Native 项目中实现 Apple HealthKit,我能够调用它并显示权限页面,但是当我允许它时,我会回到同一页面,当我授予它导航的权限时我该如何做到这一点到下一个屏幕?
这是我的代码
请求许可的页面
import React, { useRef, useEffect } from 'react';
import {
View,
FlatList,
Text,
Image,
TouchableOpacity,
Dimensions,
SafeAreaView,
} from 'react-native';
import ProgressCircle from 'react-native-progress-circle';
import Icon from 'react-native-vector-icons/AntDesign';
import styles from './styles';
import AppleHealthKit, {
HealthValue,
HealthKitPermissions,
} from 'react-native-health';
import AppleHKit from '../onboarding/applehealthkit';
import appleHealthKit from 'react-native-health';
const onboardingScreensData = [
{
title: 'NOT CONNECTED',
imagePath: require('../../assets/images/applehealthbkgoff.png'),
},
{
title: 'NOT CONNECTED',
imagePath: require('../../assets/images/applehealthbkgon.png'),
},
{
title: 'CONNECTED',
imagePath: require('../../assets/images/applehealthbkgon.png'),
},
];
const AppleHealth = ({ navigation }) => {
const screenWidth = Dimensions.get('screen').width;
const onboardingFlastListRef = useRef(null);
const renderItem = ({ item, index }) => (
<Item title={item.title} imagePath={item.imagePath} index={index} />
);
useEffect(() => {});
const Item = ({ title, imagePath, index }) => (
<View style={styles.container}>
<SafeAreaView style={{ flex: 1, flexDirection: 'column' }}>
<View
style={{ flex: 0.25, justifyContent: 'flex-end', paddingTop: 20 }}>
<Image
source={require('../../assets/images/applehealthlogo.png')}
style={{
alignSelf: 'center',
}}
resizeMode={'contain'}
/>
<Text
style={{
textAlign: 'center',
paddingHorizontal: 20,
fontSize: 30,
fontWeight: 'bold',
}}>
Apple Health
</Text>
</View>
{index == 2 ? (
<View
style={{
alignItems: 'center',
justifyContent: 'flex-start',
flex: 0.13,
}}>
<Text style={{ fontSize: 12, margin: 10, color: 'green' }}>
{title}
</Text>
</View>
) : (
<View
style={{
alignItems: 'center',
justifyContent: 'flex-start',
flex: 0.13,
}}>
<Text style={{ fontSize: 12, margin: 10, color: 'red' }}>
{title}
</Text>
</View>
)}
<View
style={{
justifyContent: 'flex-end',
alignItems: 'center',
flex: 0.6,
}}>
<Text style={{ fontSize: 25 }}>How to connect</Text>
<Text
style={{
textAlign: 'center',
marginHorizontal: 20,
marginTop: 15,
marginBottom: 20,
fontSize: 17,
}}>
Select Empatho is Apple Health Sources and tap Turn All Categories
On
</Text>
{index == 2 ? (
<View>
<TouchableOpacity
style={{
justifyContent: 'center',
backgroundColor: '#533AED',
height: 50,
borderRadius: 50,
alignItems: 'center',
width: '80%',
paddingHorizontal: 10,
}}
onPress={() => navigation.navigate('SurveyHR')}>
<View
style={{
paddingVertical: 10,
paddingHorizontal: 10,
flexDirection: 'row',
alignItems: 'center',
}}>
<Text
style={{
fontSize: 16,
color: '#FFF',
alignItems: 'center',
letterSpacing: 1,
}}>
PROCEED WITHOUT{' '}
</Text>
<Image
source={require('../../assets/images/miniapple.png')}
/>
<Text
style={{
fontSize: 16,
color: '#FFF',
alignItems: 'center',
letterSpacing: 1,
}}>
{' '}
WATCH
</Text>
</View>
</TouchableOpacity>
<TouchableOpacity onPress={() => navigation.navigate('SignIn')}>
<Text
style={{
color: '#533AED',
textAlign: 'center',
marginTop: 10,
}}>
I HAVE APPLE WATCH
</Text>
</TouchableOpacity>
</View>
) : (
<View style={{ width: screenWidth, alignItems: 'center' }}>
<TouchableOpacity
style={{
justifyContent: 'center',
backgroundColor: '#533AED',
height: 50,
borderRadius: 50,
alignItems: 'center',
width: '80%',
}}
onPress={
() => AppleHKit()
// onboardingFlastListRef.current.scrollToIndex({
// animated: true,
// index: index + 1,
// })
}>
<View
style={{
paddingVertical: 10,
paddingHorizontal: 10,
flexDirection: 'row',
alignItems: 'center',
}}>
<Text
style={{
fontSize: 16,
color: '#FFF',
alignItems: 'center',
paddingRight: 30,
paddingLeft: 40,
}}>
CONNECT
</Text>
</View>
</TouchableOpacity>
<Text
style={{
color: '#533AED',
textAlign: 'center',
marginTop: 10,
}}></Text>
</View>
)}
</View>
</SafeAreaView>
<Image
source={imagePath}
resizeMode="cover"
style={[
styles.backgroundImage,
{ position: 'absolute', bottom: 30, zIndex: -1 },
]}></Image>
</View>
);
return (
<View style={styles.container}>
<FlatList
ref={onboardingFlastListRef}
data={onboardingScreensData}
renderItem={renderItem}
keyExtractor={(item, index) => index.toString()}
horizontal={true}
decelerationRate={'fast'}
snapToInterval={screenWidth}
showsHorizontalScrollIndicator={false}
/>
</View>
);
};
export default AppleHealth;
编辑 HealthKit 权限的类
import AppleHealthKit, {
HealthValue,
HealthKitPermissions,
} from 'react-native-health';
const HealthKit = () => {
/* Permission options */
const permissions = {
permissions: {
read: [AppleHealthKit.Constants.Permissions.HeartRate],
write: [AppleHealthKit.Constants.Permissions.Steps],
},
};
AppleHealthKit.initHealthKit(permissions, (error) => {
/* Called after we receive a response from the system */
if (error) {
console.log('[ERROR] Cannot grant permissions!');
}
/* Can now read or write to HealthKit */
const options = {
startDate: new Date(2020, 1, 1).toISOString(),
};
AppleHealthKit.getHeartRateSamples(options, (callbackError, results) => {
/* Samples are now collected from HealthKit */
});
});
};
export default HealthKit;
任何人都可以在这里给我一盏灯吗?
解决方案
推荐阅读
- c++ - 为什么下面的程序不选择与第一个模板参数相同类型的参数?
- visual-studio - 连接到数据库的问题 - Sql Server 和 Visual Studio C#
- javascript - 如何在使用 Google Sheets API 4 (JavaScript) 导入期间重新排列列或指定列输出
- oop - 继承的对象在 Lua 中共享相同的表属性
- batch-file - cmd.exe 和批处理文件中的目录 /b /s 不同
- java - 从数据库向 jComboBox 添加不同的数据
- nswindow - 检测 NSWindow 的“显示标签栏”何时启用
- html - 导航栏和图像显示中间的线
- javascript - 获取模拟元数据失败(babel 7 升级)
- javascript - Bootstrap 手风琴不适用于 Angular 7