简体   繁体   中英

How can I read permission, access reading and store the values of Electrocardiogram(ECG) from HealthKit to iPhone app

As per apple documentation I go through them, after surfing in it I didn't get any of the basic implementation for electrocardiogram (ECG). As there is a basic flow in these steps:

  1. App ask for permission to allow the ECG data point.
  2. Once the permission granted, Prepare a readDataType set to handle the ECG data point function.
  3. A sample query that calculates the ECG data.

As I searched for these above points, however I didn't get an exact way to implement this.

Also how this is different from HKCategoryTypeIdentifier/HKQuantityTypeIdentifier. Because the other data points of healthkit are access and read by these type of identifier.

Any sugesstion please.

how are you? I was here facing a similar issue and found the following solution:

let sampleTypes: Set<HKSampleType> = [
            .electrocardiogramType()
        ]
        
        healthStore.requestAuthorization(toShare: nil, read: sampleTypes) {
            (success, error) in
            
            if success {
                self.beginQuery()
            }
        }

As you can see I had to put in toShare a nil parameter, that's why, at least for me, a message error pops up saying that the HKElectrocardiogramType cannot be shared by Apple's policies, even enabling in the Info it wasn't possible.

Unfortunately Apple also doesn't allow us to actually capture a new ECG while running our own watch app, so the best that I was able to do is make a query from the existing ECGs that I previously made by Apple's ECG App.

My query looks something like this:

private func beginQuery() {
        let ecgType = HKObjectType.electrocardiogramType()
        let ecgQuery = HKSampleQuery(sampleType: ecgType,
                                     predicate: nil,
                                     limit: HKObjectQueryNoLimit,
                                     sortDescriptors: nil) {
            (query, samples, error) in
            
            if let error = error {
                fatalError("Erro fatal: \(error.localizedDescription)")
            }
            
            guard let ecgSamples = samples as? [HKElectrocardiogram] else {
                fatalError("Error trying to receive samples: \(String(describing: samples))")
            }
            
            if (samples == nil) {
                print("There isn't samples to show.")
            } else {
                print("Number of available samples : \(String(describing: samples?.count))")
            }
            
            for sample in ecgSamples {
                let voltageQuery = HKElectrocardiogramQuery(sample) {
                    (query, result) in

                    switch(result) {
                    case .measurement(let measurement):
                        if let voltageQuantity = measurement.quantity(for: .appleWatchSimilarToLeadI) {
                            //This is the ACTUALLY voltage, and for me that was what I was after
                        }
                    case .done:
                        // When there is no more measures to show
                    case .error(let error):
                        print(error)
                    @unknown default:
                        // Compiler asked me include a default
                    }
                }

                self.healthStore.execute(voltageQuery)
            }
        }
        
        healthStore.execute(ecgQuery)

I know that my query snippet is almost like the one that's in apple documentation but I got in some troubles to make that even with the documentation.

I really hope that helps you @Gourav, if not, at least I hope that helps someone after me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM