Help with Pyto? (Accessing HealthKit data)

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?

have to look into it.
It should be possible. I am interested to look for a solution.

Details 1

Do document my way here

The Library Rubicon

https://rubicon-objc.readthedocs.io/en/latest/tutorial/index.html

Docs are here

https://developer.apple.com/documentation/healthkit/samples/reading_and_writing_healthkit_series_data

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…

Hope you are better at this!

We will see. Let’s take the group mind approach. :wink:

That might stop the attempt.
Let’s see how it works out.

First Inverstigation

import HealthKit


for item in dir(HealthKit):
    if 'Sleep' in item :
        print (item)
        
# TODO: Look at Object like HKSleepQuery

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!

1 Like

I will look at it on the weekend.

I have too look up that healkit issue with the app store do you have a reference link for that?

Hi, I’m trying to do the same :slight_smile: Haven’t gotten much further though…

As for the HKCategoryTypeIdentifierSleepAnalysis, this is a constant, just input it as a “string” with quotes.

Would be happy to hear if you managed to successfully request authorisation to the health store, I’m still struggling!

Since I got what I wanted from Swift, I have no longer tried to achieve this with Pyto as well.