仅从 healthkit 获取 AutoDetected 活动
Posted
技术标签:
【中文标题】仅从 healthkit 获取 AutoDetected 活动【英文标题】:Getting only AutoDetected activities from healthkit 【发布时间】:2016-08-10 07:02:13 【问题描述】:我在我的应用程序中使用 health-kit 来读取用户的步骤和活动。一切都很好,但我只想阅读自动检测到的活动和步骤。目前,我获得了手动输入或由健康应用程序自动检测到的所有数据天气。这是我目前的代码
func todaySteps(completion: (Double, NSError?) -> () )
let type = HKSampleType.quantityTypeForIdentifier(HKQuantityTypeIdentifierStepCount) // The type of data we are requesting
let date = NSDate()
print(date)
let cal = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
let newDate = cal.startOfDayForDate(date)
print(newDate)
let predicate = HKQuery.predicateForSamplesWithStartDate(newDate, endDate: NSDate(), options: .None) // Our search predicate which will fetch all steps taken today
let query = HKSampleQuery(sampleType: type!, predicate: predicate, limit: 0, sortDescriptors: nil) query, results, error in
var steps: Double = 0
if results?.count > 0
for result in results as! [HKQuantitySample]
steps += result.quantity.doubleValueForUnit(HKUnit.countUnit())
completion(steps, error)
executeQuery(query)
但是,我可以在哪里以及如何检查用户输入或自动检测到的数据?我也见过This 问题,但它在objective-c 中,我无法完全理解它,所以请指导我。
【问题讨论】:
Ignore manual entries from Apple Health app as Data Source的可能重复 该解决方案已过时,现在无法正常工作。 您能否详细说明为什么它已过时? 【参考方案1】:我自己解决了这个问题。我就是这样做的。
func todaySteps(completion: (Double, NSError?) -> () )
let type = HKSampleType.quantityTypeForIdentifier(HKQuantityTypeIdentifierStepCount)
let date = NSDate()
print(date)
let cal = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
let newDate = cal.startOfDayForDate(date)
print(newDate)
let predicate = HKQuery.predicateForSamplesWithStartDate(newDate, endDate: NSDate(), options: .None)
let query = HKSampleQuery(sampleType: type!, predicate: predicate, limit: 0, sortDescriptors: nil) query, results, error in
var steps: Double = 0
if results?.count > 0
for result in results as! [HKQuantitySample]
print("Steps \(result.quantity.doubleValueForUnit(HKUnit.countUnit()))")
// checking and truncating manually added steps
if result.metadata != nil
// Theses steps were entered manually
else
// adding steps to get total steps of the day
steps += result.quantity.doubleValueForUnit(HKUnit.countUnit())
completion(steps, error)
executeQuery(query)
【讨论】:
以上是关于仅从 healthkit 获取 AutoDetected 活动的主要内容,如果未能解决你的问题,请参考以下文章