[英]Health kit - Get shared health data along with my health data
I can able to get my health data list from health kit.我可以从健康包中获取我的健康数据列表。 I used below code to get my today's step count:
我使用下面的代码来获取我今天的步数:
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, _ in
guard let result = result, let sum = result.sumQuantity() else {
completion(0.0)
return
}
completion(sum.doubleValue(for: HKUnit.count()))
}
healthStore.execute(query)
}
But i have one more health data of my brother, where he invited me to see his health data in my real device.但是我还有一个我兄弟的健康数据,他邀请我在我的真实设备中查看他的健康数据。 Now i was not able to read / get his health data.
现在我无法读取/获取他的健康数据。 Is there any way to get that.
有没有办法得到它。
Any solution would be great...!任何解决方案都会很棒......!
You don't want a statics query if you want the step count.如果您想要步数,则不需要静态查询。 Try this;
尝试这个;
let comp: DateComponents = Calendar.current.dateComponents([.year, .month], from: Date())
let startOfMonth = Calendar.current.date(from: comp)!
searchPredicate = HKQuery.predicateForSamples(withStart: startOfMonth, end: Date(), options: .strictStartDate)
let sampleQuery = HKSampleQuery(sampleType: .stepCount, predicate: searchPredicate, limit: limit, sortDescriptors: [])
{
(query, result, error) in
if error != nil
{
completion(-1)
}
self.hkSampleRecs.removeAll(keepingCapacity: true)
if result != nil
{
for r in result!
{
self.hkSampleRecs.append(r)
}
}
completion(self.hkSampleRecs.count)
}
healthStore.execute(sampleQuery)
Note that I haven't shown you the completion setup I used.请注意,我没有向您展示我使用的完成设置。 I'm also storing the records in an array I defined elsewhere (hkSampleRecs).
我还将记录存储在我在其他地方定义的数组(hkSampleRecs)中。 This example also gives data only for this month so far.
此示例还提供了迄今为止仅本月的数据。 Change the dates as you need.
根据需要更改日期。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.