HurtByCode
HurtByCode

Reputation: 51

How do i read Tracking Cycle data (Period prediction date) from health using healthkit?

I need the period prediction data from health on my apps, but i don't know how to get it. i already tried to get the menstrual flow with HKQuery but that's not the data that i wanted. i just can't find the period prediction data (date format).

    private func readmens() {
            
            let cycle = HKSampleType.categoryType(forIdentifier: HKCategoryTypeIdentifier.menstrualFlow)
            let today = Date()
            let startDate = Calendar.current.date(byAdding: .month, value: -3, to: today)
            let predicate = HKQuery.predicateForSamples(withStart: startDate, end: today, options: HKQueryOptions.strictEndDate)
            let query = HKSampleQuery.init(sampleType: cycle!, predicate: predicate, limit: HKObjectQueryNoLimit, sortDescriptors: nil) { (query, results, error) in print("Menstrual Cycle : ", results) }
            HKHealthStore().execute(query)
}

Upvotes: 3

Views: 689

Answers (1)

bshkrva
bshkrva

Reputation: 31

Unfortunately, Apple does not provide direct access to period prediction data through HealthKit. The only information you can retrieve is the recorded menstrual flow or range of days when the user logged their cycle. However, Apple does provide the start date of the last menstrual cycle. Based on this data, you can calculate the expected start of the next cycle, fertile window, and ovulation using average cycle length or by allowing users to set this information in your app. For more accurate predictions, you might need to track several cycles to establish a baseline for prediction.

Upvotes: 1

Related Questions