iOS HealthKit,微信修改步数,QQ修改步数

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的可以写入步数

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 近日,看到糯米粉写的文章iOS 教你如何修改微信运动步数 ,趁着五一放假,也想来玩一下改微信运动步数,占领一下朋友...
    我系哆啦阅读 159,932评论 14 68
  • 现在很多软件都加上了运动模块,比如QQ和微信,而且还有排行榜,可以和好友比较谁的运动步数多,任何东西只要添加了比较...
    idage阅读 21,481评论 19 32
  • 昨天上QQ玩QQ步数的时候发现很多小伙伴的步数高的吓人,而我仅仅可怜的三位数,于是我就想我怎么能把这个步数刷刷,这...
    CoderZNB阅读 8,321评论 6 3
  • [iOS] HealthKit 获取步数 前言 HealthKit 是苹果在 iOS 8.0 之后推出的健康框架,...
    李小六_阅读 1,396评论 0 7
  • 文:耳双生 我们已走得太远,以至于忘记了来时的路。 无从考据这句话是谁开始用的,似乎是一首歌的歌词。 这句话,细想...
    耳双生阅读 346评论 0 0