xcode objective-c iOS-获取健康运动步数 - NSLog(@&#34

📅 2026-12-01 👤 拼多多助力营销技巧 📂 拼多多助力 👁️ 159 次阅读

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

获取健康运动数据前,需要在该key下添加HealhtKit功能 在项目info.plist里添加授权描述,目前只用到了获取步数的权限。 开始引入#import获取步数问题。

#import

@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

精彩链接

标签: 拼多多助力网站平台 赚钱项目 拼多多助力网 首码项目 手赚APP
拼多多助力营销技巧