昨天上QQ玩QQ步数的时候发现很多小伙伴的步数高的吓人,而我仅仅可怜的三位数,于是我就想我怎么能把这个步数刷刷,这样我就能占领整个QQ封面了--是时候装一波逼了😄,这不仅仅可以装逼,还可以做公益,大家应该都有玩过捐步吧,那可都是money啊,为了公益事业,我想这个应用必须得写了!!!!!
捐步截图
- 捐步截图.PNG
用过苹果机都知道,苹果有个健康应用,其实第三方的应用都是从这里拿的步行数据,当然,不仅仅是步数数据,还有很多,以下都算
- IMG_0010.PNG
怎么才能修改步数呢
答:HealthKit
什么是HealthKit
简单的说,HealthKit就是iOS8 以后出现的,苹果用来生成,存储,查询各种健康数据的一个API,包括iPhone本身创建的健身数据,或者第三方app创建的健康数据,都可以通过这个API进行读取和查询.也可以把HealthKit看成iPhone的健康数据的一个统一的数据库,同一个手机上的不用app的健康数据的读取都是直接面向healthKit,由HealthKit统一管理,来实现iOS上不同应用之间的健康数据的交互.微信(包括qq或者其他第三方app)上的运动步数,本质上也是通过HealthKit来读取的,所以,我们只需要新建一个app(当然,我这里只做开发环境),请求对HealthKit数据的写入权限,添加运动步数后,(qq或者其他第三方app)通过HealthKit读取我们手机上的健康数据中的运动步数后,自然读取后的运动步数就可以由我们随心所欲来修改了.当然,healthKit里面包含各种各种的健康数据,包括步数,睡眠,运动距离,卡路里,血压等等.想要查看这些数据非常简单,打开iPhone里面苹果自带的健康应用,非常直观的展示了我们的健康数据.
怎么用HealthKit
这部分内容不打算写了,其实没多少内容,这里附上我参考的链接:点击进入
核心代码
// 1创建 healthKitStore 对象
self.healthKitStore = [[HKHealthStore alloc] init];
// 2 创建 基于HKSampleType的健康对像
// 2.1创建 height 类型
HKSampleType *height = [HKSampleType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeight];
NSSet *healthDataToRead = [NSSet setWithArray:@[height]];
HKSampleType *runing = [HKSampleType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];
NSSet *healthDataToWrite = [NSSet setWithArray:@[runing]];
// 3请求授权
// 3.1第一个参数可写
// 3.2第二个参数可读
// 3.3第三个参数授权回调
[_healthKitStore requestAuthorizationToShareTypes:healthDataToWrite readTypes:healthDataToRead completion:^(BOOL success, NSError * _Nullable error) {
if (success) {
NSLog(@"授权成功");
[self reloadData];
}
}];
- (void)reloadData {
HKQuantityType *stepType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];
[self fetchSumOfSamplesTodayForType:stepType unit:[HKUnit countUnit] completion:^(double stepCount, NSError *error) {
NSLog(@"%f",stepCount);
dispatch_async(dispatch_get_main_queue(), ^{
_stepCount.text = [NSString stringWithFormat:@"%.f",stepCount];
});
}];
}
- (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.healthKitStore executeQuery:query];
}
- (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];
}
- (IBAction)writeStepcount:(UIButton *)sender {
[self addstepWithStepNum:_textF.text.doubleValue];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[self.view endEditing:YES];
}
- (void)addstepWithStepNum:(double)stepNum {
HKQuantitySample *stepCorrelationItem = [self stepCorrelationWithStepNum:stepNum];
[self.healthKitStore 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 reloadData];
}
else {
NSLog(@"The error was: %@.", error);
UIAlertView *doneAlertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"添加失败" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[doneAlertView show];
return ;
}
});
}];
}
- (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];
HKDevice *device = [[HKDevice alloc] initWithName:@"iPhone" manufacturer:@"Apple" model:@"iPhone 7 (Model 1660, 1778, 1779, 1780)" hardwareVersion:@"iPhone7" firmwareVersion:@"9.2" softwareVersion:@"10.2 (14C92)" localIdentifier:@"ZNBmm" UDIDeviceIdentifier:@"e5f56af41988fe84497f179dbccdfc081c7bd101"];
HKQuantitySample *stepConsumedSample = [HKQuantitySample quantitySampleWithType:stepConsumedType quantity:stepQuantityConsumed startDate:startDate endDate:endDate device:device metadata:nil];
return stepConsumedSample;
}
写在最后
为什么写这篇文章--------->
装逼算不算呢!1111111当然算,除此之外,也算是给小伙伴们一个思路吧,也为以后入Healthkit坑做好准备,加油,技术就是金钱啊,要是钱都是捐给我的话多好啊