我正在创建一个应用程序,在那里我可以阅读健康套件中的数据。我可以读取步数、跑步+步行等。现在我正在尝试读取骑自行车的日期和持续时间。这是我在跑步+步行中使用的
func readDistanceWalkingRunning(completion: (([AnyObject]!, NSError!) -> Void)!) {
let runningWalking = HKQuantityType.quantityTypeForIdentifier(HKQuantityTypeIdentifierDistanceWalkingRunning)
let predicate = HKQuery.predicateForSamplesWithStartDate(NSDate().dateByAddingTimeInterval(-86400.0), endDate: NSDate(), options: HKQueryOptions.None)
let stepsSampleQuery = HKSampleQuery(sampleType: runningWalking!,
predicate: predicate,
limit: 100,
sortDescriptors: nil)
{ [weak self] (query, results, error) in
if let results = results as? [HKQuantitySample] {
for result in results {
print(" Distance was " + " \(result.quantity.doubleValueForUnit(HKUnit.mileUnit())) ")
print("Date was " + "\(result.startDate)")
}
}
}
// Don't forget to execute the Query!
executeQuery(stepsSampleQuery)
}
一切都很好,但当我尝试使用下面的代码读取自行车的距离时,我得到的结果为零。自行车数据显示在appleHealth应用程序中,但为什么我得到的结果为零?请帮助
func readDistanceCycling(completion: (([AnyObject]!, NSError!) -> Void)!) {
let distanceCycling = HKQuantityType.quantityTypeForIdentifier(HKQuantityTypeIdentifierDistanceCycling)
let predicate = HKQuery.predicateForSamplesWithStartDate(NSDate(), endDate: NSDate(), options: HKQueryOptions.None)
let query = HKSampleQuery(sampleType: distanceCycling!, predicate: predicate, limit: 100, sortDescriptors: nil, resultsHandler: { query, result, error in
if result != nil
{
print("We have some Data")
}
else
{
print("Result is nil")
}
if let results = result as? [HKQuantitySample] {
for result in results {
print(" Quantity type " + " \(result.quantityType) ")
print("Date was " + "\(result.startDate)")
}
}
})
executeQuery(query)
}
}
1条答案
按热度按时间idv4meu81#
使用下面给出的函数。它给你最少7条记录。根据你的要求,你也可以改变它。
}
使用上面的函数如下。这里你需要传递一个类型和单位。