Skip to main content

Usage: Sync health data manually

Sync Apple Health data by yourself.

info

The automatic implementation is faster and easier to integrate and is what most developers need, try to use it before attempting to create a custom sync process.

Sync health data manually​

There are 2 types of health data Summaries and Events.

Health DataTimezoneOldest date of retrievalLatest date of retrievalClass
SummaryUTCN/AYesterdayAHRookSyncManager
EventUTCN/ATodayAHRookSyncManager

Sync summaries​

Use sync(bool enableLogs) to sync the last 29 days of SLEEP_SUMMARY, PHYSICAL_SUMMARY and BODY_SUMMARY (not including today).

warning

If the app goes to the background the synchronization will fail, use scheduleYesterdaySync. instead if you want the synchronization to continue until it finishes (scheduleYesterdaySync will not only sync summaries, but also events).

void syncSummariesHistoric() async {
try {
await AHRookSyncManager.sync(enableLogs: isDebug);

// Historic summaries sync started
} catch (error) {
// Handle error
}
}

Use sync(DateTime date) to sync SLEEP_SUMMARY, PHYSICAL_SUMMARY and BODY_SUMMARY for the provided date.

void syncSummaries() async {
try {
await AHRookSyncManager.sync(date: date);

// Summaries synced successfully
} catch (error) {
// Handle error
}
}

Use sync(DateTime date, AHSummarySyncType summary) to sync the summary of choice for the provided date.

void syncSingleSummary() async {
try {
await AHRookSyncManager.sync(date: date, summary: summarySyncType);

// Summary synced successfully
} catch (error) {
// Handle error
}
}

Sync events​

Use syncEvents(DateTime date, AHEventSyncType event) to sync the event of choice for the provided date.

void syncSingleEvent() async {
try {
await AHRookSyncManager.syncEvents(date, eventSyncType);

// Event synced successfully
} catch (error) {
// Handle error
}
}

Sync current day events​

Current day events allow to send small amounts of health data, but they also return the data that was sent, you can use this data to update your UI without waiting for the webhook event.

Steps events​

Retrieve and upload current day steps count of Apple Health.

void syncStepsEvents() async {
try {
final steps = await AHRookSyncManager.getTodayStepsCount();

// Update UI
} catch (error) {
// Handle error
}
}

Calories events​

Retrieve and upload current day calories count of Apple Health.

void syncCaloriesEvents() async {
try {
final calories = await AHRookSyncManager.getTodayCaloriesCount();

// Update UI
} catch (error) {
// Handle error
}
}

Heart Rate events​

Retrieve and upload current day heart rate of Apple Health.

void syncHeartRateEvents() async {
try {
final heartRate = await AHRookSyncManager.getTodayHeartRate();

// Update UI
} catch (error) {
// Handle error
}
}

Sync and display​

If you want to sync and get a copy of the summary/event that was uploaded you can call any of the following functions:

Sleep Summary​

void example() async {
final date = DateTime.now();

try {
final sleepSummaries = await AHRookSyncManager.getSleepSummary(date);

// Update UI
} catch (error) {
// Handle error (for apple health records not found can be returned as an error)
}
}

Physical Summary​

void example() async {
final date = DateTime.now();

try {
final physicalSummary = await AHRookSyncManager.getPhysicalSummary(date);

// Update UI
} catch (error) {
// Handle error (for apple health records not found can be returned as an error)
}
}

Body Summary​

void example() async {
final date = DateTime.now();

try {
final bodySummary = await AHRookSyncManager.getBodySummary(date);

// Update UI
} catch (error) {
// Handle error (for apple health records not found can be returned as an error)
}
}

Activity event​

void example() async {
final date = DateTime.now();

try {
final activityEvents = await AHRookSyncManager.getActivityEvents(date);

// Update UI
} catch (error) {
// Handle error (for apple health records not found can be returned as an error)
}
}