昨天上QQ玩QQ步數(shù)的時(shí)候發(fā)現(xiàn)很多小伙伴的步數(shù)高的嚇人,而我僅僅可憐的三位數(shù),于是我就想我怎么能把這個(gè)步數(shù)刷刷,這樣我就能占領(lǐng)整個(gè)QQ封面了--是時(shí)候裝一波逼了??,這不僅僅可以裝逼,還可以做公益,大家應(yīng)該都有玩過捐步吧,那可都是money啊,為了公益事業(yè),我想這個(gè)應(yīng)用必須得寫了!!!!!
捐步截圖
用過蘋果機(jī)都知道,蘋果有個(gè)健康應(yīng)用,其實(shí)第三方的應(yīng)用都是從這里拿的步行數(shù)據(jù),當(dāng)然,不僅僅是步數(shù)數(shù)據(jù),還有很多,以下都算
怎么才能修改步數(shù)呢
答:HealthKit
什么是HealthKit
簡(jiǎn)單的說,HealthKit就是iOS8 以后出現(xiàn)的,蘋果用來生成,存儲(chǔ),查詢各種健康數(shù)據(jù)的一個(gè)API,包括iPhone本身創(chuàng)建的健身數(shù)據(jù),或者第三方app創(chuàng)建的健康數(shù)據(jù),都可以通過這個(gè)API進(jìn)行讀取和查詢.也可以把HealthKit看成iPhone的健康數(shù)據(jù)的一個(gè)統(tǒng)一的數(shù)據(jù)庫(kù),同一個(gè)手機(jī)上的不用app的健康數(shù)據(jù)的讀取都是直接面向healthKit,由HealthKit統(tǒng)一管理,來實(shí)現(xiàn)iOS上不同應(yīng)用之間的健康數(shù)據(jù)的交互.微信(包括qq或者其他第三方app)上的運(yùn)動(dòng)步數(shù),本質(zhì)上也是通過HealthKit來讀取的,所以,我們只需要新建一個(gè)app(當(dāng)然,我這里只做開發(fā)環(huán)境),請(qǐng)求對(duì)HealthKit數(shù)據(jù)的寫入權(quán)限,添加運(yùn)動(dòng)步數(shù)后,(qq或者其他第三方app)通過HealthKit讀取我們手機(jī)上的健康數(shù)據(jù)中的運(yùn)動(dòng)步數(shù)后,自然讀取后的運(yùn)動(dòng)步數(shù)就可以由我們隨心所欲來修改了.當(dāng)然,healthKit里面包含各種各種的健康數(shù)據(jù),包括步數(shù),睡眠,運(yùn)動(dòng)距離,卡路里,血壓等等.想要查看這些數(shù)據(jù)非常簡(jiǎn)單,打開iPhone里面蘋果自帶的健康應(yīng)用,非常直觀的展示了我們的健康數(shù)據(jù).
怎么用HealthKit
這部分內(nèi)容不打算寫了,其實(shí)沒多少內(nèi)容,這里附上我參考的鏈接:點(diǎn)擊進(jìn)入
核心代碼
// 1創(chuàng)建 healthKitStore 對(duì)象
self.healthKitStore = [[HKHealthStore alloc] init];
// 2 創(chuàng)建 基于HKSampleType的健康對(duì)像
// 2.1創(chuàng)建 height 類型
HKSampleType *height = [HKSampleType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeight];
NSSet *healthDataToRead = [NSSet setWithArray:@[height]];
HKSampleType *runing = [HKSampleType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];
NSSet *healthDataToWrite = [NSSet setWithArray:@[runing]];
// 3請(qǐng)求授權(quán)
// 3.1第一個(gè)參數(shù)可寫
// 3.2第二個(gè)參數(shù)可讀
// 3.3第三個(gè)參數(shù)授權(quán)回調(diào)
[_healthKitStore requestAuthorizationToShareTypes:healthDataToWrite readTypes:healthDataToRead completion:^(BOOL success, NSError * _Nullable error) {
if (success) {
NSLog(@"授權(quán)成功");
[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當(dāng)然算,除此之外,也算是給小伙伴們一個(gè)思路吧,也為以后入Healthkit坑做好準(zhǔn)備,加油,技術(shù)就是金錢啊,要是錢都是捐給我的話多好啊