Created
May 17, 2021 10:41
-
-
Save fakeheal/efc2e5a4626aa7d2a99f22fd17e7da0e to your computer and use it in GitHub Desktop.
Example: Request read/write Steps and Query Data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useState } from 'react' | |
import { SafeAreaView, ScrollView, StatusBar, StyleSheet, Text, View } from 'react-native' | |
import { Colors } from 'react-native/Libraries/NewAppScreen' | |
import AppleHealthKit, { HealthKitPermissions, HealthValue } from 'react-native-health' | |
/* Permission options */ | |
const permissions = { | |
permissions: { | |
read: [AppleHealthKit.Constants.Permissions.Steps], | |
write: [AppleHealthKit.Constants.Permissions.Steps], | |
}, | |
} as HealthKitPermissions | |
AppleHealthKit.initHealthKit(permissions, (error: string) => { | |
/* Called after we receive a response from the system */ | |
if (error) { | |
console.log('[ERROR] Cannot grant permissions!') | |
} | |
/* Can now read or write to HealthKit */ | |
AppleHealthKit.getStepCount( | |
{ | |
date: new Date().toISOString(), // optional; default now | |
includeManuallyAdded: false, // optional: default true | |
}, | |
(err: Object, results: HealthValue) => { | |
if (err) { | |
return | |
} | |
console.log(results) | |
}, | |
) | |
}) | |
export default function App() { | |
const [authStatus, setAuthStatus] = useState<any>({}) | |
const handlePressGetAuthStatus = () => { | |
AppleHealthKit.getAuthStatus(permissions, (err, result) => { | |
if (err) { | |
console.error(err) | |
} | |
setAuthStatus(result) | |
}) | |
} | |
return ( | |
<> | |
<StatusBar barStyle='dark-content' /> | |
<SafeAreaView> | |
<ScrollView | |
contentInsetAdjustmentBehavior='automatic' | |
style={styles.scrollView}> | |
<View style={styles.body}> | |
<View style={styles.sectionContainer}> | |
<Text style={styles.sectionTitle}> | |
React Native Health Example | |
</Text> | |
<Text onPress={handlePressGetAuthStatus}> | |
Press me to get Auth Status | |
</Text> | |
<Text style={styles.sectionDescription}> | |
{JSON.stringify(authStatus, null, 2)} | |
</Text> | |
</View> | |
</View> | |
</ScrollView> | |
</SafeAreaView> | |
</> | |
) | |
} | |
const styles = StyleSheet.create({ | |
scrollView: { | |
backgroundColor: Colors.lighter, | |
}, | |
engine: { | |
position: 'absolute', | |
right: 0, | |
}, | |
body: { | |
backgroundColor: Colors.white, | |
}, | |
sectionContainer: { | |
marginTop: 32, | |
paddingHorizontal: 24, | |
}, | |
sectionTitle: { | |
fontSize: 24, | |
fontWeight: '600', | |
color: Colors.black, | |
}, | |
sectionDescription: { | |
marginTop: 8, | |
fontSize: 18, | |
fontWeight: '400', | |
color: Colors.dark, | |
}, | |
highlight: { | |
fontWeight: '700', | |
}, | |
footer: { | |
color: Colors.dark, | |
fontSize: 12, | |
fontWeight: '600', | |
padding: 4, | |
paddingRight: 12, | |
textAlign: 'right', | |
}, | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment