昨天上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坑做好準備,加油,技術就是金錢啊,要是錢都是捐給我的話多好啊