網上有一些基于逆向工程的應用的方案中,是直接在微信頭文件中發(fā)現(xiàn),WCDeviceStepObject這個類里面有很顯眼的屬性: m7StepCount, hkStepCount, 一猜就是用來記錄運動步數(shù)的屬性, 然后直接編寫Tweak.xm,修改m7StepCount的get方法,直接返回一個運動步數(shù),就可以修改自己微信的運動步數(shù)了。
由于本人對逆向工程了解的不多,所以這篇文章主要是學習蘋果的healthKit框架,打通各種第三方app和蘋果本身健康應用之間的數(shù)據(jù)通道,實現(xiàn)各種健康數(shù)據(jù)在第三方應用和蘋果健康應用之間的讀寫。

HealthKit框架:
1.HKUnit: 由于健康數(shù)據(jù)包括各種各樣數(shù)據(jù)類型,HealthKit對這些數(shù)據(jù)進行了統(tǒng)一規(guī)范,HKUnit包括了Mass,Length,Volume,Pressure,Time,Energy,Temperature,Electrical Conductance,Scalar等各種數(shù)據(jù)類型.
[HKUnit countUnit]
2.HKQuantity:?通過數(shù)據(jù)類型HKUnit,把我們數(shù)學意義上的數(shù)量轉換成HealthKit的數(shù)據(jù).
HKQuantity *stepQuantityConsumed = [HKQuantity quantityWithUnit:[HKUnit countUnit] doubleValue:stepNum];quantityWithUnit:poundUnit doubleValue:weight];
3.HKObjectType: 健康數(shù)據(jù)的類型,包括運動步數(shù),心率,卡路里等等.
HKObjectType一般都根據(jù)identifier來創(chuàng)建的
+(nullable HKQuantityType)quantityTypeForIdentifier:(NSString)identifier;
+(nullable HKCategoryType)categoryTypeForIdentifier:(NSString)identifier;
+(nullable HKCharacteristicType)characteristicTypeForIdentifier:(NSString)identifier;
創(chuàng)建運動步數(shù)的HKObject的例子:
NSDate?endDate = [NSDate date];
NSDatestartDate = [NSDate dateWithTimeInterval:-300 sinceDate:endDate];
HKQuantitystepQuantityConsumed = [HKQuantity quantityWithUnit:[HKUnit countUnit] doubleValue:stepNum];
HKQuantityType stepConsumedType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];
4.HKHealthStore: healthKit的管理器,用來鏈接到數(shù)據(jù)庫,保存和查詢數(shù)據(jù),在app中必須一直被持有(should be long lived).類似下面的操作均是HKHealthStore的對象方法.
- (void)saveObject:(HKObject)object withCompletion:(void(^)(BOOL success, NSError__nullable error))completion;
- (void)executeQuery:(HKQuery)query;
- (void)deleteObject:(HKObject)object withCompletion:(void(^)(BOOL success, NSError * __nullable error))completion;
5.HKQuery: healthKit數(shù)據(jù)的查詢
-(instancetype)initWithSampleType:(HKSampleType)sampleType?predicate:(nullable NSPredicate)predicate?limit:(NSUInteger)limit?sortDescriptors:(nullable NSArray)sortDescriptors?resultsHandler:(void(^)(HKSampleQueryquery, NSArray<__kindof HKSample *>__nullable results, NSError__nullable error))resultsHandler;
修改運動步數(shù):?要修改微信或者其他第三方app的健康數(shù)據(jù),只需要修改蘋果中HKHealthStore的數(shù)據(jù)即可,有些第三方app是讀取HKHealthStore的數(shù)據(jù)的.(有些對數(shù)據(jù)來源做了限制,例如:微信)
根據(jù)日期讀取數(shù)據(jù):



