1. plist配置權(quán)限
NSHealthShareUsageDescription:讀取用戶健康數(shù)據(jù)
NSHealthUpdateUsageDescription:更改用戶健康數(shù)據(jù)
2. 設(shè)備支持與授權(quán)
HealthKit是iOS8加入的API
HealthKit在iPad上不可用
通過HKHealthStore類方法 + (BOOL)isHealthDataAvailable;判斷設(shè)備是否支持HealthKit
BOOL isSupport = [HKHealthStore isHealthDataAvailable];
if (isSupport) {
HKQuantityType *weightType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMass];
HKQuantityType *BodyFat = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyFatPercentage];
/// 體重和體脂率
NSSet *set = [NSSet setWithObjects:weightType, BodyFat,nil];
HKHealthStore *healthStore = [[HKHealthStore alloc]init];
/// ShareTypes寫入權(quán)限申請(qǐng) readTypes讀取權(quán)限申請(qǐng)
[healthStore requestAuthorizationToShareTypes:set readTypes:set completion:^(BOOL success, NSError * _Nullable error) {
}];
}
3. 健康數(shù)據(jù)的寫入與讀取
以體重為例,將體重寫入至健康,
健康類型對(duì)象構(gòu)建——HKQuantitySample quantitySampleWithType::::
- quantityType:數(shù)據(jù)類型(體重)
- quantity:值類型(kg)
- startDate:起始時(shí)間(NSDate)
- endDate:結(jié)束時(shí)間(NSDate),對(duì)于非持續(xù)性的數(shù)據(jù)類型用同一個(gè)就行函荣。
寫入方法——HKQuantitySample saveObject:
- saveObject:健康類型的數(shù)據(jù)對(duì)象
HKHealthStore * healthStore = [[HKHealthStore alloc] init];
HKQuantityType *weightType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMass];
HKQuantity *weightQuantity = [HKQuantity quantityWithUnit:[HKUnit unitFromString:@"kg"] doubleValue:69.3];
theDate = [NSDate date];
HKQuantitySample *weightSample = [HKQuantitySample quantitySampleWithType:weightType quantity:weightQuantity startDate:theDate endDate:theDate];
[healthStore saveObject:weightSample withCompletion:^(BOOL success, NSError *error) {
if (!success) {
NSLog(@"An error occured saving the weight sample %@. In your app, try to handle this gracefully. The error was: %@.", weightSample, error);
return ;
}
}];
數(shù)據(jù)讀取有多種方式,我主要說下HKSampleQuery(樣本查詢)和HKStatisticsCollectionQuery(統(tǒng)計(jì)集合查詢)
HKSampleQuery 這是使用最多的查詢磅甩。使用樣本查詢來讀取任何類型的樣本數(shù)據(jù)假残。當(dāng)你想要對(duì)結(jié)果進(jìn)行排序或者限制返回的樣本總數(shù)時(shí)米间,樣本查詢就特別有用滩援。更多信息栅隐,參見 HKSampleQuery Class Reference
樣本查詢——HKSampleQuery initWithSampleType
- SampleType:查詢值類型(體重)
- predicate:查詢條件(起始-結(jié)束時(shí)間)
- limit:最大結(jié)果數(shù)(HKObjectQueryNoLimit-不作限制)
- sortDescriptors:結(jié)果排序(時(shí)間正序)
- resultsHandler:回調(diào),一般需求遍歷results就行
- (void)testHKSampleQuery {
HKHealthStore *healthStore = [[HKHealthStore alloc]init];
HKQuantityType *sampleType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMass];
NSDate *now = [NSDate date];
NSDate *endDate = [now dateTools_dateByAddingDays:1];
NSDate *startDate = [endDate dateTools_dateByAddingDays:-100];
NSPredicate *pre = [HKQuery predicateForSamplesWithStartDate:startDate endDate:endDate options:HKQueryOptionStrictStartDate];
/// 測(cè)試了下key用HKSampleSortIdentifierEndDate 和上述pre的options不一致 也有正常結(jié)果
NSSortDescriptor *start = [NSSortDescriptor sortDescriptorWithKey:HKSampleSortIdentifierStartDate ascending:YES];
HKSampleQuery *sampleQuery = [[HKSampleQuery alloc] initWithSampleType:sampleType predicate:pre limit:HKObjectQueryNoLimit sortDescriptors:@[start] resultsHandler:^(HKSampleQuery * _Nonnull query, NSArray<__kindof HKSample *> * _Nullable results, NSError * _Nullable error) {
for (HKQuantitySample *result in results) {
double weight = [result.quantity doubleValueForUnit:[HKUnit unitFromString:@"kg"]];
NSString *value = [NSString stringWithFormat:@"%.2f",weight];
NSString *createTime = [NSString stringWithFormat:@"%ld", (long)[result.startDate timestamp]];
NSLog(@"====value:%@====createTime:%@====", value, createTime);
}
}];
/// 執(zhí)行查詢
[healthStore executeQuery:sampleQuery];
}
HKStatisticsCollectionQuery玩徊,使用這種查詢來在一系列長(zhǎng)度固定的時(shí)間間隔中執(zhí)行多次統(tǒng)計(jì)查詢租悄。通常使用這種查詢來生成圖表。查詢提供了一些簡(jiǎn)單的方法來計(jì)算某些值恩袱,例如泣棋,每天消耗的總熱量或者每5分鐘行走的步數(shù)。統(tǒng)計(jì)集合查詢是長(zhǎng)時(shí)間運(yùn)行的畔塔。查詢可以返回當(dāng)前的統(tǒng)計(jì)集合外傅,也可以監(jiān)測(cè)HealthKit存儲(chǔ)纪吮,并對(duì)更新做出響應(yīng)。更多信息萎胰,參見 HKStatisticsCollectionQuery Class Reference 。
統(tǒng)計(jì)集合查詢——HKStatisticsCollectionQuery initWithQuantityType
- quantityType:查詢值類型(體重)
- quantitySamplePredicate:查詢條件(起始-結(jié)束時(shí)間)
- options:用于定義執(zhí)行的統(tǒng)計(jì)計(jì)算的類型以及合并來自多個(gè)源的數(shù)據(jù)的方式
- HKStatisticsOptionSeparateBySource 數(shù)據(jù)來源統(tǒng)計(jì)
- HKStatisticsOptionDiscreteAverage 平均值統(tǒng)計(jì)
- HKStatisticsOptionDiscreteMin 最小值統(tǒng)計(jì)
- HKStatisticsOptionDiscreteMax 最大值統(tǒng)計(jì)
- HKStatisticsOptionCumulativeSum 和統(tǒng)計(jì)
- HKStatisticsOptionMostRecent 最近的值
- anchorDate:統(tǒng)計(jì)數(shù)據(jù)時(shí)間間隔的定位時(shí)間(星期一上午12:00)
- intervalComponents:統(tǒng)計(jì)數(shù)據(jù)的時(shí)間間隔(3天)
- (void)tesHKStatisticsCollectionQuery {
HKHealthStore *healthStore = [[HKHealthStore alloc]init];
// 數(shù)據(jù)類型
HKQuantityType *type = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMass];
// 獲取數(shù)據(jù)的截止時(shí)間 今天
NSDate *endDate = [NSDate date];
// 獲取數(shù)據(jù)的起始時(shí)間 此處取從今日往前一年的數(shù)據(jù)
NSDate *startDate = [NSDate dateWithTimeIntervalSinceNow:-365*24*60*60];
// 查詢條件棚辽,用于獲取設(shè)置時(shí)間段內(nèi)的數(shù)據(jù)
NSPredicate *predicate = [HKQuery predicateForSamplesWithStartDate:startDate endDate:endDate options:HKQueryOptionStrictStartDate];
// 設(shè)置時(shí)間支持單位
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *anchorComponents = [calendar components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear | NSCalendarUnitWeekday fromDate:[NSDate date]];
NSInteger offset = (7 + anchorComponents.weekday - 2) % 7;
/// 日期設(shè)置為星期一上午12:00 也就是第一個(gè)統(tǒng)計(jì)數(shù)據(jù)的開始時(shí)間為星期一12點(diǎn)
anchorComponents.day -= offset;
anchorComponents.hour = 12;
NSDate *anchorDate = [calendar dateFromComponents:anchorComponents];
// 統(tǒng)計(jì)數(shù)據(jù)的時(shí)間間隔(一定要比0大)技竟,例如設(shè)為3,則返回的統(tǒng)計(jì)數(shù)據(jù)跨度為3天屈藐,例如2020-07-10 —— 2020-07-13
NSDateComponents *intervalComponents = [[NSDateComponents alloc] init];
intervalComponents.day = 3;
HKStatisticsCollectionQuery *query = [[HKStatisticsCollectionQuery alloc] initWithQuantityType:type quantitySamplePredicate:predicate options:HKStatisticsOptionSeparateBySource | HKStatisticsOptionDiscreteAverage anchorDate:anchorDate intervalComponents:intervalComponents];
query.initialResultsHandler = ^(HKStatisticsCollectionQuery *query, HKStatisticsCollection *result, NSError *error) {
NSLog(@"%@",[result statistics]);
for (HKStatistics *sample in [result statistics]) {
double weight = [sample.averageQuantity doubleValueForUnit:[HKUnit unitFromString:@"kg"]];
NSString *value = [NSString stringWithFormat:@"%.2f",weight];
NSLog(@"====averageValue:%@====startDate:%@======", value, sample.startDate);
}
};
[healthStore executeQuery:query];
}
4. 健康數(shù)據(jù)的修改
健康的數(shù)據(jù)只有添加和刪除榔组,所謂修改就是刪除一條再添加一條。
我這里的思路联逻,刪除的依據(jù)是時(shí)間搓扯,對(duì)體重?cái)?shù)據(jù)的修改只能修改數(shù)值,不能修改時(shí)間包归,所以查出同時(shí)間的數(shù)據(jù)锨推,刪除舊的,再添加新的公壤。
deleteObject的對(duì)象和saveObject雖然類型一致换可,但是倘若按照saveObject的方法去構(gòu)建,是不能成功刪除的厦幅,所以經(jīng)測(cè)試后只能先查詢沾鳄,再刪除。
- (void)testUpdate {
HKHealthStore * healthStore = [[HKHealthStore alloc] init];
HKQuantityType *weightType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMass];
/// 已添加體重的時(shí)間
NSDate *theDate = [NSDate dateWithTimeIntervalSince1970:1594608650];
/// 修改后的數(shù)值
double weight = 75;
HKQuantity *weightQuantity = [HKQuantity quantityWithUnit:[HKUnit unitFromString:@"kg"] doubleValue:weight];
HKQuantitySample *weightSample = [HKQuantitySample quantitySampleWithType:weightType quantity:weightQuantity startDate:theDate endDate:theDate];
///查詢條件 指定時(shí)間~指定時(shí)間+1S 查詢條數(shù)為1
NSPredicate *pre = [HKQuery predicateForSamplesWithStartDate:theDate endDate:[theDate dateTools_dateByAddingSeconds:1] options:HKQueryOptionStrictStartDate];
NSSortDescriptor *start = [NSSortDescriptor sortDescriptorWithKey:HKSampleSortIdentifierStartDate ascending:YES];
HKSampleQuery *sampleQuery = [[HKSampleQuery alloc] initWithSampleType:weightType predicate:pre limit:1 sortDescriptors:@[start] resultsHandler:^(HKSampleQuery * _Nonnull query, NSArray<__kindof HKSample *> * _Nullable results, NSError * _Nullable error) {
//打印查詢結(jié)果
NSLog(@"%@",results);
if (results.count > 0) {
HKQuantitySample *result = results.firstObject;
[healthStore deleteObject:result withCompletion:^(BOOL success, NSError * _Nullable error) {
if (!success) {
NSLog(@"An error occured delete the weight sample %@. In your app, try to handle this gracefully. The error was: %@.", weightSample, error);
}
[healthStore saveObject:weightSample withCompletion:^(BOOL success, NSError *error) {
if (!success) {
NSLog(@"An error occured saving the weight sample %@. In your app, try to handle this gracefully. The error was: %@.", weightSample, error);
}
}];
}];
} else {
[healthStore saveObject:weightSample withCompletion:^(BOOL success, NSError *error) {
if (!success) {
NSLog(@"An error occured saving the weight sample %@. In your app, try to handle this gracefully. The error was: %@.", weightSample, error);
}
}];
}
}];
[healthStore executeQuery:sampleQuery];
}