//MARK : HealthStore func getData() -> Double { //Get Authorization let stepType = HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.stepCount)! let heartRate = HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.heartRate)! let spo2 = HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.respiratoryRate)! healthStore.requestAuthorization(toShare: [], read: [stepType,heartRate,spo2]) { (chk, error) in if (chk){ print("Permission Granted") var dumm = 0.0 //currentHeartRate(completion: nil) self.getSteps(currentDate: Date()) { result in globalString.todayStepsCount = result dumm = result print(result) } return dumm } } } //getData func getSteps(currentDate: Date, completion: @escaping (Double) -> Void) { guard let sampleType = HKCategoryType.quantityType(forIdentifier: .stepCount) else { print("Get Steps not executed as is null") return } let now = currentDate //Date() let startDate = Calendar.current.startOfDay(for: now) let predicate = HKQuery.predicateForSamples(withStart: startDate, end: now, options: .strictEndDate) let query = HKStatisticsQuery(quantityType: sampleType, 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())) }
healthStore.execute(query) }