我正在使用 iHealth 设备从使用 iHealth 应用程序的设备获取生命体征,这些数据存储在 Health 应用程序中。我已将我的应用程序配置为与 Health 应用程序通信,但我不知道如何将存储的健康数据获取到我自己的自定义应用程序。
由于没有此问题的示例,并且文档也没有提供有关它的深入信息。
我正在使用 iHealth 设备从使用 iHealth 应用程序的设备获取生命体征,这些数据存储在 Health 应用程序中。我已将我的应用程序配置为与 Health 应用程序通信,但我不知道如何将存储的健康数据获取到我自己的自定义应用程序。
由于没有此问题的示例,并且文档也没有提供有关它的深入信息。
只要您(或您的应用程序的用户)已授予您的应用程序访问权限以读取 iHealth 存储在 HealthKit 数据库中的内容,您就可以使用 HealthKit API 查询它。
// 1. these are the items we want to read
let healthKitTypesToRead = NSSet(array:[
HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierBodyMass),
HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierBloodGlucose)
])
// 2. these are the items we want to write
let healthKitTypesToWrite = NSSet(array:[
HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierBodyMass),
HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierBloodGlucose)
])
// 3. Request HealthKit authorization
healthKitStore!.requestAuthorizationToShareTypes(healthKitTypesToWrite, readTypes: healthKitTypesToRead) { (success, error) -> Void in
if( completion != nil )
{
if (success == true) {
self.initialized = true
}
completion(success:success,error:error)
}
}
然后就可以查询数据了:
// 4. Build the Predicate
let past = NSDate.distantPast() as NSDate
let now = NSDate()
let mostRecentPredicate = HKQuery.predicateForSamplesWithStartDate(past, endDate:now, options: .None)
// Build the sort descriptor to return the samples in descending order
let sortDescriptor = NSSortDescriptor(key:HKSampleSortIdentifierStartDate, ascending: false)
// we want to limit the number of samples returned by the query to just 1 (the most recent)
let limit = 1
// Build samples query
let sampleQuery = HKSampleQuery(sampleType: sampleType, predicate: mostRecentPredicate, limit: limit, sortDescriptors: [sortDescriptor])
{ (sampleQuery, results, error ) -> Void in
if let queryError = error {
completion(nil,error)
return;
}
// Get the first sample
let mostRecentSample = results.first as? HKQuantitySample
// Execute the completion closure
if completion != nil {
completion(mostRecentSample,nil)
}
}
// 5. Execute the Query
self.healthKitStore!.executeQuery(sampleQuery)
}
仅供参考,在您的 AppID 中启用 Healthkit 后。您必须从健康商店验证需求。
let healthKitStore:HKHealthStore = HKHealthStore()
func authorizeHealthKit(completion: ((success:Bool, error:NSError!) -> Void)!)
{
// 1. Set the types you want to read from HK Store
var healthKitTypesToRead = self.dataTypesToRead() as! Set<NSObject>
// 2. Set the types you want to write to HK Store
var healthKitTypesToWrite = self.dataTypesToWrite() as! Set<NSObject>
// 3. If the store is not available (for instance, iPad) return an error and don't go on.
if !HKHealthStore.isHealthDataAvailable()
{
let error = NSError(domain: "com.domain.....", code: 2, userInfo: [NSLocalizedDescriptionKey:"HealthKit is not available in this Device"])
if( completion != nil )
{
completion(success:false, error:error)
}
return;
}
else
{
// 4. Request HealthKit authorization
healthKitStore.requestAuthorizationToShareTypes(healthKitTypesToWrite, readTypes:healthKitTypesToRead, completion: { (success, error) -> Void in
if( completion != nil )
{
completion(success:success,error:error)
}
})
}
}
有用的链接如下:
https://en.wikipedia.org/wiki/Health_%28application%29
http://www.raywenderlich.com/86336/ios-8-healthkit-swift-getting-started