I thought I could not get the HealthKit capability in Xcode with a Personal Team, but apparently I can, so I could implement what I needed in Swift after all:
let healthStore = HKHealthStore()
let sleepType = HKObjectType.categoryType(forIdentifier: HKCategoryTypeIdentifier.sleepAnalysis)!
func authorize() {
let toShare = Set<HKSampleType>()
let read = Set<HKSampleType>([sleepType])
healthStore.requestAuthorization(toShare: toShare, read: read) { _, _ in }
}
func retrieveSleepAnalysis() {
let sortDescriptor = NSSortDescriptor(key: HKSampleSortIdentifierEndDate, ascending: false)
let query = HKSampleQuery(sampleType: sleepType,
predicate: nil,
limit: HKObjectQueryNoLimit,
sortDescriptors: [sortDescriptor]) { _, samples, error in
guard error == nil, let samples = samples else { return }
samples.forEach({
if let sample = $0 as? HKCategorySample {
let value = (sample.value == HKCategoryValueSleepAnalysis.inBed.rawValue) ? "InBed" : "Asleep"
let source = sample.sourceRevision.source.name
print("\(sample.startDate)-\(sample.endDate): \(value) [\(source)]")
}
})
}
healthStore.execute(query)
}
Would still be nice if we could get this to work in Pyto though!