iOS获取健康的运动步数,需要注意筛选下用户手动编辑录入的数据,HKMetadataKeyWasUserEntered
为1时,为手动录入数据。
#import <HealthKit/HealthKit.h>
@interface TestGetSteps ()
@property (nonatomic, strong) HKHealthStore *hkHStore; /**< 健康数据 */
@property (nonatomic, strong) HKObjectType *hkOType; /**< 获取的权限 */
@property (nonatomic, strong) HKSampleType *hkSType; /**< 获取采样数据类型 */
@end
@implementation TestGetSteps
- (void)handleInitGetSteps {
if ([HKHealthStore isHealthDataAvailable]) { // 检查是否支持获取健康数据
self.hkHStore = [[HKHealthStore alloc] init];
self.hkOType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount]; //获取步数类型
NSSet *stepSet = [NSSet setWithObject:self.hkOType];
__weak typeof(self) weakSelf = self;
[self.hkHStore requestAuthorizationToShareTypes:nil readTypes:stepSet completion:^(BOOL success, NSError * _Nullable error) { // 获取步数授权
if (success) {
[weakSelf handleGetWalkSteps];
}else {
NSLog(@"status:%@ message:%@", @(error.code), error.domain);
}
}];
}else {
NSLog(@"message:%@", @"设备不支持HealthKit功能");
}
}
// 获取当天时间开始跟结束: 00:00:00 ~ 23:59:59
- (NSDate *)handleGetDateWithCaledar:(NSCalendar *)calendar nowDate:(NSDate *)nowDate hour:(NSInteger)hour minute:(NSInteger)minute second:(NSInteger)second {
NSDateComponents *dateNowComponents = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:nowDate];
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
[dateComponents setDay:dateNowComponents.day];
[dateComponents setMonth:dateNowComponents.month];
[dateComponents setYear:dateNowComponents.year];
[dateComponents setHour:hour];
[dateComponents setMinute:minute];
[dateComponents setSecond:second];
return [calendar dateFromComponents:dateComponents];
}
// 获取步数值
- (void) handleGetWalkSteps {
self.hkSType = [HKSampleType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];
NSSortDescriptor *startSortDec = [NSSortDescriptor sortDescriptorWithKey:HKSampleSortIdentifierStartDate ascending:NO];
NSSortDescriptor *endSortDec = [NSSortDescriptor sortDescriptorWithKey:HKSampleSortIdentifierEndDate ascending:NO];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *nowDate = [[NSDate alloc] initWithTimeIntervalSinceNow:0];
NSDate *startDate = [self handleGetDateWithCaledar:calendar nowDate:nowDate hour:0 minute:0 second:0];
NSDate *endDate = [self handleGetDateWithCaledar:calendar nowDate:nowDate hour:23 minute:59 second:59];
NSPredicate *predicate = [HKQuery predicateForSamplesWithStartDate:startDate endDate:endDate options:HKQueryOptionNone]; // 筛选当天的数据
__weak typeof(self) weakSelf = self;
HKSampleQuery *hkSQ = [[HKSampleQuery alloc] initWithSampleType:self.hkSType predicate:predicate limit:HKObjectQueryNoLimit sortDescriptors:@[startSortDec, endSortDec] resultsHandler:^(HKSampleQuery * _Nonnull query, NSArray<__kindof HKSample *> * _Nullable results, NSError * _Nullable error) {
DEBUGLog(@"stepCounts:%@ results:%@ error:%@", query, results, error);
HKUnit *unit = [HKUnit unitFromString:@"count"];// 看返回数据结构,应该这里步数的unit可以通过count字符串来实现。
NSInteger allSteps = 0;
for (HKQuantitySample *sampM in results) {
NSInteger isUserWrite = [sampM.metadata[HKMetadataKeyWasUserEntered] integerValue];
if (isUserWrite == 1) { // 用户手动录入的数据。
}else {
double steps = [sampM.quantity doubleValueForUnit:unit];
NSInteger stepIntegrs = (NSInteger)steps;
allSteps += stepIntegrs;
}
}
NSLog(@"获取步数成功:%ld", (long)allSteps);
}];
[self.hkHStore executeQuery:hkSQ]; // 执行
/**
返回数据格式
{
HKSample = {
HKObject = {
NSObject = {
isa = HKCumulativeQuantitySample
}
_UUID = xxx
_sourceRevision = xxx
_device = nil
_metadata = 0x000000028291c940 1 key/value pair
_provenanceID = 0
_sourceBundleIdentifier = nil
_creationTimestamp = 675566015.49851298
_contributor = nil
}
_sampleType = 0x000000028257aca0
_startTimestamp = 675565980
_endTimestamp = 675565980
}
_quantity = 0x000000028291f2e0
_freezeState = 2
_count = 1
_codableQuantitySample = nil
}
*/
}
@end
原文地址:https://blog.csdn.net/qiluoyiyi/article/details/125059566
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_24162.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。