1、HealthKit的授權(quán)
//獲取讀取的授權(quán)
lethealthKitTypesToRead =NSSet(array:[
//出生日期
HKObjectType.characteristicTypeForIdentifier(HKCharacteristicTypeIdentifierDateOfBirth)!,
//血液類(lèi)型
HKObjectType.characteristicTypeForIdentifier(HKCharacteristicTypeIdentifierBloodType)!,
//性別
HKObjectType.characteristicTypeForIdentifier(HKCharacteristicTypeIdentifierBiologicalSex)!,
//體重
HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierBodyMass)!,
//身高
HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeight)!,
HKObjectType.workoutType()
])
//獲取寫(xiě)出的授權(quán)蓄坏,也是需要表明相關(guān)的寫(xiě)入對(duì)象
lethealthKitTypeToWrite =NSSet.init(array: [
HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierBodyMassIndex)!,
HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierActiveEnergyBurned)!,
HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierDistanceWalkingRunning)!,
HKQuantityType.workoutType()
])
將你想使用的相應(yīng)的數(shù)據(jù)裝入集合中澄者。
驗(yàn)證健康數(shù)據(jù)是否可用
if !HKHealthStore.isHealthDataAvailable(){
//TO DO SOME
}
開(kāi)始授權(quán)
let healthKitStore:HKHealthStore=HKHealthStore()
healthKitStore.requestAuthorizationToShareTypes(healthKitTypeToWrite as? Set, readTypes: healthKitTypesToRead as? Set) { (success, error) in {}
2、獲取相關(guān)信息
對(duì)于一些簡(jiǎn)單的數(shù)據(jù)(非集合的)如:個(gè)人的基本信息集嵌∩倨郏可以直接通過(guò)HKHealthStore提供的相關(guān)方法直接獲得
let healthKitStore:HKHealthStore=HKHealthStore()
//e.g
var age: Int
let birthDay: NSDate?
do{
birthDay =tryhealthKitStore.dateOfBirth()
}catch{
birthDay =nil
print("read Date Data faild")
}
ifbirthDay !=nil
{
lettoday =NSDate()
letdifferenceComponents =NSCalendar.currentCalendar().components(.NSYearCalendarUnit, fromDate: birthDay!, toDate: today, options:NSCalendarOptions(rawValue:0))
age = differenceComponents.year
}
而對(duì)于一些數(shù)據(jù)集組成的數(shù)據(jù)來(lái)說(shuō)需要借助HKSampleQuery來(lái)進(jìn)行相關(guān)的查詢(xún)
@method initWithSampleType:predicate:limit:sortDescriptors:resultsHandler:
@abstract Returns a query that will retrieve HKSamples matching the given predicate.
@param sampleType ? The type of sample to retrieve.
@param predicate ? The predicate which samples should match.
@param limit ? The maximum number of samples to return.Pass HKObjectQueryNoLimit for no limit.
@param sortDescriptors ? The sort descriptors to use to order the resulting samples.
@param resultsHandler ?The block to invoke with results when the query has finished executing.
*/
public init(sampleType:HKSampleType, predicate:NSPredicate?, limit:Int, sortDescriptors: [NSSortDescriptor]?, resultsHandler: (HKSampleQuery, [HKSample]?,NSError?) ->Void)
}
其上的方法通過(guò)相應(yīng)的參數(shù)來(lái)控制查詢(xún)結(jié)果
//sampleType:HKSampleType
let sampleType =HKSampleType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeight)
通過(guò)identifier來(lái)控制查詢(xún)的是那一類(lèi)的數(shù)據(jù)
通過(guò)predicate:NSPredicate來(lái)進(jìn)行相關(guān)的篩選
//e.g 關(guān)于NSDate的篩選
//endDate為返回結(jié)果中的屬性之一
NSPredicate*predicate_date =
[NSPredicatepredicateWithFormat:@"endDate >= %@ AND endDate <= %@", today,afterDay];
通過(guò)sortDescriptors: [NSSortDescriptor]?來(lái)進(jìn)行排序處理
通過(guò)limit:Int來(lái)限制最大的數(shù)據(jù)個(gè)數(shù)
//今天(04.11)就先到這點(diǎn)吧正压。瓷式。替饿。
//第一次發(fā)完善中吧