Swift HealthKit 연동

2022. 5. 31. 15:47아이폰 개발

For swift 4.2

1) Get HealthKitPermission

import HealthKit

func getHealthKitPermission() {

    delay(0.1) {
        guard HKHealthStore.isHealthDataAvailable() else {
            return
        }

        let stepsCount = HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.stepCount)!

        self.healthkitStore.requestAuthorization(toShare: [], read: [stepsCount]) { (success, error) in
            if success {
                print("Permission accept.")
            }
            else {
                if error != nil {
                    print(error ?? "")
                }
                print("Permission denied.")
            }
        }
    }
}

2) To get steps count for specific date

func getStepsCount(forSpecificDate:Date, completion: @escaping (Double) -> Void) {
        let stepsQuantityType = HKQuantityType.quantityType(forIdentifier: .stepCount)!
        let (start, end) = self.getWholeDate(date: forSpecificDate)

        let predicate = HKQuery.predicateForSamples(withStart: start, end: end, options: .strictStartDate)

        let query = HKStatisticsQuery(quantityType: stepsQuantityType, quantitySamplePredicate: predicate, options: .cumulativeSum) { _, result, _ in
            guard let result = result, let sum = result.sumQuantity() else {
                completion(0.0)
                return
            }
            completion(sum.doubleValue(for: HKUnit.count()))
        }

        self.healthKitStore.execute(query)
    }

    func getWholeDate(date : Date) -> (startDate:Date, endDate: Date) {
        var startDate = date
        var length = TimeInterval()
        _ = Calendar.current.dateInterval(of: .day, start: &startDate, interval: &length, for: startDate)
        let endDate:Date = startDate.addingTimeInterval(length)
        return (startDate,endDate)
    }

How to use

self.getStepsCount(forSpecificDate: Date()) { (steps) in
                if steps == 0.0 {
                    print("steps :: \(steps)")
                }
                else {
                    DispatchQueue.main.async {
                        print("steps :: \(steps)")
                    }
                }
            }
 

HealthKit Swift getting today's steps

I am making a swift iOS app that integrates with a user's step count as reported by the Health app. I can easily find the user's step count in the last hour, using this as my predicate: let

stackoverflow.com

https://stackoverflow.com/questions/39596622/nshealthshareusagedescription-must-be-set-in-the-apps-info-plist-in-order-to-re

 

NSHealthShareUsageDescription must be set in the app's Info.plist in order to request read authorization

I have a framework that uses _healthStore requestAuthorizationToShareTypes:writeTypes readTypes:readTypes completion:^(BOOL success, NSError *error) { When I include the framework into my iOS ap...

stackoverflow.com

https://stackoverflow.com/questions/25993314/healthkit-error-missing-com-apple-developer-healthkit-entitlement

 

HealthKit error: Missing com.apple.developer.healthkit entitlement

I'm adding code for HealthKit in my iOS Swift app, but I'm getting an error: /* Ask for permission to access the health store */ override func viewDidAppear(animated: Bool) { super.viewDidAppear(

stackoverflow.com

https://stackoverflow.com/questions/25993314/healthkit-error-missing-com-apple-developer-healthkit-entitlement

 

HealthKit error: Missing com.apple.developer.healthkit entitlement

I'm adding code for HealthKit in my iOS Swift app, but I'm getting an error: /* Ask for permission to access the health store */ override func viewDidAppear(animated: Bool) { super.viewDidAppear(

stackoverflow.com

 

 

https://exchangetuts.com/entitlements-file-was-modified-during-the-build-which-is-not-supported-1639708035005319

 

Entitlements file was modified during the build, which is not supported

I'm getting following error during release build: error: Entitlements file "projectname.entitlements" was modified during the build, which is not supported.

exchangetuts.com