Any Pyto users on this forum? (thank you for mentioning it, @tf2)
Maybe someone can help me with using its Objective-C bridge to access HealthKit data?
If possible at all… They list HealthKit as one of the modules that can be imported, but I know @simonbs once shared that HealthKit support was removed from Scriptable because of an App Store (or TestFlight?) rejection. And since Apple treats all developers equal I would be surprised if we can get this to work in Pyto…
I’m already stuck on requesting authorization (defining the types I want to access):
How can I get to the constant HKCategoryTypeIdentifierSleepAnalysis to supply to HKObjectType.categoryTypeForIdentifier in Pyto?
I read both sets of documentation before posting here and I have done something similar years ago natively (can’t remember whether I was using Objective-C or Swift back then), but I currently don’t have an Apple Developer
Program where I can add the HealthKit permission myself.
In fact, that’s my biggest worry: that Pyto does not have that capability either.
Getting the authorization to work is probably the best way to find out, but as posted I got stuck even on what should be a relatively simple thing…
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!