iOS-获取健康运动步数

iOS获取健康的运动步数,需要注意筛选下用户手动编辑录入的数据,HKMetadataKeyWasUserEntered 为1时,为手动录入数据。

  1. 获取健康运动数据前,需要在该key下添加HealhtKit功能
    identifier添加HealthKit功能

  2. 在项目info.plist里添加授权描述,目前只用到了获取步数的权限。
    获取健康数据授权描述

  3. 开始引入#import <HealthKit/HealthKit.h> 获取步数问题。

#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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值