iOS-获取手机健康步数,去除人为添加的数据

时间:2021-08-31 15:43:56

<span style="font-size:18px;">#pragma mark - 获取步数从HealthKit
- (void)getstepCountFromHealthKit
{
HKQuantityType *type = self.items[5];
//这个函数是获取苹果健康里面的全部步数包括人为添加的
[self fetchQuantity:typecompletionHandler:^(NSArray *result,NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
if (error)
{
//从苹果健康获取步数失败
NSLog(@"error:%@",error);
[PersonInfo sharePersonInfo].isAllow = NO;
}
else if ([resultcount] == 0)
{
//没有步数
[PersonInfo sharePersonInfo].isAllow = YES;
}
// succeeded to retrieve the health data
else
{
[PersonInfo sharePersonInfo].isAllow = YES;
//这个函数是去除人为添加的,留下系统记录的步数
NSArray *array = [selfgetRealHealthData:result];

//统计数组里面的步数
NSInteger allArrStepCount = 0;
//一个数组的添加
for (NSDictionary *dictin array)
{
NSInteger stepCount = [dict[@"stepCount"]integerValue];
allArrStepCount = allArrStepCount + stepCount;
NSString *dateTime = dict[@"dateTime"];
[_dateTimeArr addObject:dateTime];
}

NSString *value = [NSStringstringWithFormat:@"%ld",(long)allArrStepCount];

[selfsetStepCountAboutDataBase:value locationCount:1getArr:array];
}
});
}];
}


#pragma mark - 从健康中获取真实数据 --去除人为添加的
- (NSArray *)getRealHealthData:(NSArray *)resultArr
{
NSMutableArray *returnArr = [[NSMutableArrayalloc]init];
HKQuantityType *stepType = [HKObjectTypequantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];
for (HKQuantitySample *modelin resultArr)
{
//HKQuantity 类存储了给定单位的值,此值和单位就构成了数据。和 HKUnit类一样,在使用它时,需要进行实例化。实例化 HKQuantity需要使用到 init(unit:doubleValue:)方法。它可以用来创建一个 quantity(数量)对象。
HKQuantity *resultQuantity = model.quantity;
HKUnit *unit = [TTMHealthKitHelperdefaultUnitForQuantityType:stepType];
double valueDou = [resultQuantity doubleValueForUnit:unit];
NSString *value = [NSStringstringWithFormat:@"%.0f",valueDou];

//NSString *startDateStr = [RequestHelper getDateStrFromDate:model.startDate];
NSString *endDateStr = [RequestHelpergetDateStrFromDate:model.endDate];

NSDictionary *dict = (NSDictionary *)model.metadata;
NSInteger wasUserEntered = [dict[@"HKWasUserEntered"]integerValue];

if(wasUserEntered == 1)
{
//这是用户自己手动添加的数据
//NSLog(@"value == %@",value);
}
else
{
//这是苹果HealthKit记录的数据
NSDictionary *dictReturn = @{@"dateTime":endDateStr,@"stepCount":value};
[returnArr addObject:dictReturn];
}
}
return returnArr;
}</span>


相关类下载:http://download.csdn.net/detail/u014220518/9626411