1.Linked Frameworks and Libraries 里添加 HealthKit.framework
2.需要一个开发者账号,在生成描述文件的时候,选中HealthKit
3.代码实现:
UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, 320, 40)];
[self.view addSubview:lable];
HKHealthStore *health= [[HKHealthStore alloc] init];
NSSet *helathkit =[NSSet setWithArray:[NSArray arrayWithObjects: [HKObjectType quantityTypeForIdentifier:@"HKQuantityTypeIdentifierStepCount"], nil]];
;
[health requestAuthorizationToShareTypes:helathkit readTypes:helathkit completion:^(BOOL tt,NSError *error){
/**
* @author 付强, 16-05-13 13:05:43
*
* 写入健康数据
*
* @return return value description
*/
HKQuantityType *ty = [HKQuantityType quantityTypeForIdentifier:@"HKQuantityTypeIdentifierStepCount"];
NSDate *date = [NSDate date];
HKQuantity *witre = [HKQuantity quantityWithUnit:[HKUnit countUnit] doubleValue:80000.0];
HKQuantitySample *samplequan = [HKQuantitySample quantitySampleWithType:ty quantity:witre startDate:date endDate:date];
[health saveObject:samplequan withCompletion:^(BOOL tt,NSError *error){
if (tt) {
NSLog(@"写入成功");
}
}];
/**
获取健康数据
- returns: return value description
*/
HKSampleType *sampleType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];
NSSortDescriptor *start = [NSSortDescriptor sortDescriptorWithKey:HKSampleSortIdentifierStartDate ascending:NO];
NSSortDescriptor *end = [NSSortDescriptor sortDescriptorWithKey:HKSampleSortIdentifierEndDate ascending:NO];
NSDate *now = [NSDate date];
NSDateFormatter *dateBJ=[[NSDateFormatter alloc] init];
[dateBJ setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *str = [dateBJ stringFromDate:now];
str = [str substringToIndex:10];
NSString *startda = [NSString stringWithFormat:@"%@ 00:00:00",str];
NSDate *d=[dateBJ dateFromString:startda];
NSPredicate *predicate = [HKQuery predicateForSamplesWithStartDate:d endDate:now options:HKQueryOptionNone];
HKSampleQuery *sampleQuery = [[HKSampleQuery alloc] initWithSampleType:sampleType predicate:predicate limit:0 sortDescriptors:@[start,end] resultsHandler:^(HKSampleQuery * _Nonnull query, NSArray<__kindof HKSample *> * _Nullable results, NSError * _Nullable error) {
NSInteger num = 0;
for (int i = 0; i < results.count; i++) {
HKQuantitySample *result = results[i];
NSString *das = (NSString *)result;
NSString *dass = [NSString stringWithFormat:@"%@",das];
NSString *endtime = [dass substringFromIndex:dass.length - 25];
NSString *startt = [dass substringFromIndex:dass.length -51];
NSString *startime = [startt substringToIndex:25];
if (![endtime isEqualToString:startime]) {
HKQuantity *quantity = result.quantity;
NSString *stepStr = (NSString *)quantity;
NSString *sad = [NSString stringWithFormat:@"%@ ",stepStr];
sad = [sad substringToIndex:sad.length - 7];
NSInteger geshu = [sad integerValue];
num = num + geshu;
}
}
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
//查询是在多线程中进行的,如果要对UI进行刷新,要回到主线程中
// NSLog(@"最新步数:%@",stepStr);
lable.text = [NSString stringWithFormat:@"你的步数%ld",(long)num];
}];
}];
[health executeQuery:sampleQuery];
}];
4.大功告成,目前写入微信步数不好用。但是QQ的可以写入步数