iOS通过HealthKit来修改步数(添加)

2018-09-17  本文已影响278人  Smallwolf_JS

最近微信的步数排名,支付宝的步数,QQ的步数霸占首页等等,好像很火的样子!

- (HKHealthStore *)healthStore{
    if (!_healthStore) {
        _healthStore = [[HKHealthStore alloc]init];
    }
    return _healthStore;
}
//获取权限
    HKQuantityType *stepCountType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];
    NSSet *writeDataTypes = [NSSet setWithObjects:stepCountType, nil];
    [self.healthStore requestAuthorizationToShareTypes:writeDataTypes readTypes:nil completion:^(BOOL success, NSError * _Nullable error) {
        
        if (success) {
            //修改步数
            HKQuantityType * quantityTypeIdentifier = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];
            HKQuantity *quantity = [HKQuantity quantityWithUnit:[HKUnit countUnit] doubleValue:30000.0f];
            HKQuantitySample *temperatureSample = [HKQuantitySample quantitySampleWithType:quantityTypeIdentifier quantity:quantity startDate:[NSDate date] endDate:[NSDate date] metadata:nil];
            
            [self.healthStore saveObject:temperatureSample withCompletion:^(BOOL success, NSError * _Nullable error) {
                if (success) {
                    dispatch_async(dispatch_get_main_queue(), ^{
                        NSLog(@"添加成功");
                    });
                }else{
                    dispatch_async(dispatch_get_main_queue(), ^{
                        NSLog(@"添加失败");
                    });
                }
            }];
        }
    }];

然后你就可以在你的健康应用里面查看添加的步数了


希望我的代码可以帮助到大家。

上一篇 下一篇

猜你喜欢

热点阅读