我正在制作健康应用程序。我想walkingRunningDistance
从HealthKit
Swift 中获得。但是,我有一个问题。返回值为 0.0 英里。
为什么返回值为 0 英里?
我的代码是这样的。
func recentSteps3(completion: (Double, NSError?) -> () ){
let type = HKSampleType.quantityTypeForIdentifier(HKQuantityTypeIdentifierDistanceWalkingRunning)
let date = NSDate()
let cal = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
let newDate = cal.startOfDayForDate(date)
let predicate = HKQuery.predicateForSamplesWithStartDate(newDate, endDate: NSDate(), options: HKQueryOptions.StrictStartDate)
let query = HKSampleQuery(sampleType: type!, predicate: predicate, limit: HKObjectQueryNoLimit, sortDescriptors: nil) { query, results, error in
var distance: Double = 0
if results?.count > 0
{
for result in results as! [HKQuantitySample]
{
distance += result.quantity.doubleValueForUnit(HKUnit.mileUnit())
}
}
completion(distance, error)
}
healthAuth.healthStore.executeQuery(query)
}