美文网首页
iOS修改步数

iOS修改步数

作者: Apple技术产品粉 | 来源:发表于2017-11-11 21:42 被阅读0次

只能修改健康应用中的步数哦

1)导入头文件

#import <HealthKit/HealthKit.h>

2)初始化全局变量

@property (nonatomic) HKHealthStore *healthStore;

3)获取步数 刷新界面

- (void)getStepsFromHealthKit{

HKQuantityType *stepType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];

[self fetchSumOfSamplesTodayForType:stepType unit:[HKUnit countUnit] completion:^(double stepCount, NSError *error) {

dispatch_async(dispatch_get_main_queue(), ^{

_readStepLabel.text = [NSString stringWithFormat:@"%.f",stepCount];

});

}];

}

4)读取HealthKit数据

- (void)fetchSumOfSamplesTodayForType:(HKQuantityType *)quantityType unit:(HKUnit *)unit completion:(void (^)(double, NSError *))completionHandler {

NSPredicate *predicate = [self predicateForSamplesToday];

HKStatisticsQuery *query = [[HKStatisticsQuery alloc] initWithQuantityType:quantityType quantitySamplePredicate:predicate options:HKStatisticsOptionCumulativeSum completionHandler:^(HKStatisticsQuery *query, HKStatistics *result, NSError *error) {

HKQuantity *sum = [result sumQuantity];

if (completionHandler) {

double value = [sum doubleValueForUnit:unit];

completionHandler(value, error);

}

}];

[self.healthStore executeQuery:query];

}

5)NSPredicate数据模型

- (NSPredicate *)predicateForSamplesToday {

NSCalendar *calendar = [NSCalendar currentCalendar];

NSDate *now = [NSDate date];

NSDate *startDate = [calendar startOfDayForDate:now];

NSDate *endDate = [calendar dateByAddingUnit:NSCalendarUnitDay value:1 toDate:startDate options:0];

return [HKQuery predicateForSamplesWithStartDate:startDate endDate:endDate options:HKQueryOptionStrictStartDate];

}

6)添加步数

- (void)addstepWithStepNum:(double)stepNum {

HKQuantitySample *stepCorrelationItem = [self stepCorrelationWithStepNum:stepNum];

[self.healthStore saveObject:stepCorrelationItem withCompletion:^(BOOL success, NSError *error) {

dispatch_async(dispatch_get_main_queue(), ^{

if (success) {

[self.view endEditing:YES];

UIAlertView *doneAlertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"添加成功" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];

[doneAlertView show];

//刷新数据  重新获取步数

[self getStepsFromHealthKit];

}else {

NSLog(@"The error was: %@.", error);

UIAlertView *doneAlertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"添加失败" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];

[doneAlertView show];

return ;

}

});

}];

}

7)获取HKQuantitySample数据模型

- (HKQuantitySample *)stepCorrelationWithStepNum:(double)stepNum {

NSDate *endDate = [NSDate date];

NSDate *startDate = [NSDate dateWithTimeInterval:-300 sinceDate:endDate];

HKQuantity *stepQuantityConsumed = [HKQuantity quantityWithUnit:[HKUnit countUnit] doubleValue:stepNum];

HKQuantityType *stepConsumedType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];

NSString *strName = [[UIDevice currentDevice] name];

NSString *strModel = [[UIDevice currentDevice] model];

NSString *strSysVersion = [[UIDevice currentDevice] systemVersion];

NSString *localeIdentifier = [[NSLocale currentLocale] localeIdentifier];

HKDevice *device = [[HKDevice alloc] initWithName:strName manufacturer:@"Apple" model:strModel hardwareVersion:strModel firmwareVersion:strModel softwareVersion:strSysVersion localIdentifier:localeIdentifier UDIDeviceIdentifier:localeIdentifier];

HKQuantitySample *stepConsumedSample = [HKQuantitySample quantitySampleWithType:stepConsumedType quantity:stepQuantityConsumed startDate:startDate endDate:endDate device:device metadata:nil];

return stepConsumedSample;

}

相关文章

网友评论

      本文标题:iOS修改步数

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