美文网首页
iOS HealthKit,微信修改步数,QQ修改步数

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

作者: 客官大大 | 来源:发表于2016-08-31 16:18 被阅读0次

    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 HealthKit,微信修改步数,QQ修改步数

          本文链接:https://www.haomeiwen.com/subject/wtzqettx.html