美文网首页
iOS-获取运动健康步数

iOS-获取运动健康步数

作者: 丿小七 | 来源:发表于2022-05-31 10:09 被阅读0次

    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
    
    

    相关文章

      网友评论

          本文标题:iOS-获取运动健康步数

          本文链接:https://www.haomeiwen.com/subject/secrprtx.html