gpt4 book ai didi

ios - 快速从 healthkit 中获取周、月和年格式的用户数据

转载 作者:搜寻专家 更新时间:2023-11-01 07:06:26 25 4
gpt4 key购买 nike

我需要一些帮助来实现我的应用程序中的 HealthKit。我正在使用 Swift 4 Xcode 9。我正在使用以下代码来获取特定日期的用户步数:

func getTodaysSteps(completion: @escaping (Double) -> Void) {
let stepsQuantityType = HKQuantityType.quantityType(forIdentifier: .stepCount)!

let now = Date()
let startOfDay = Calendar.current.startOfDay(for: now)
let predicate = HKQuery.predicateForSamples(withStart: startOfDay, end: now, options: .strictStartDate)

let query = HKStatisticsQuery(quantityType: stepsQuantityType, quantitySamplePredicate: predicate, options: .cumulativeSum) { (_, result, error) in
var resultCount = 0.0

guard let result = result else {
// log.error("Failed to fetch steps = \(error?.localizedDescription ?? "N/A")")
completion(resultCount)
return
}

if let sum = result.sumQuantity() {
resultCount = sum.doubleValue(for: HKUnit.count())
}

DispatchQueue.main.async {
completion(resultCount)
}
}

healthStore.execute(query)
}

现在我想获取整个月中每一天的用户步数和卡路里。换句话说,我想让用户以周、月和年的格式计算过去的步数。谁能帮我做同样的事情?

最佳答案

您只需要设置开始日期(在您的情况下可能是 aWeekAgo、aMothAgo、aYearAgo)并且 toDate 是当前日期

let sevneDaysAgo = NSCalendar.current.date(byAdding: .day, value: -7, to: Date())

let currentDate = Date.init()

PedometerManager.shared.getPedometerDataFromDate(fromDate: sevneDaysAgo, toDate: currentDate) { [weak self] (data, error, errorMsg) in
if(error == nil && data != nil) {

if let count = data?.numberOfSteps {

}

if let distance = data?.distance {

let roundDis = round(distance.doubleValue)
let dis = String.init(format: "%.3f",roundDis)
}


if let pace = data?.currentPace {

}

if let cadence = data?.currentCadence {

}

if let ascend = data!.floorsAscended {

}

if let desc = data!.floorsDescended {

}

if let activity = self?.activityName {

}


}
}

你必须使用 CMPedometer 类

关于ios - 快速从 healthkit 中获取周、月和年格式的用户数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47829264/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com