如何下载Healthkit步骤和距离数据?

时间:2014-11-11 00:13:12

标签: iphone health-kit

我想下载运动处理器在iPhone 5S(及更高版本)中收集的步长距离数据,并在Apple的HealthKit中提供,以供分析。

最简单/最好的方法是什么?

澄清(在新答案之后):有没有办法在没有编写新的iOS应用的情况下做到这一点?是否存在提供数据的现有应用程序,和/或提供访问权限的任何iCloud API。

3 个答案:

答案 0 :(得分:4)

我不确定它能帮到你,但这就是我的步骤

+ (void)readUsersStepFromHK:(NSDate*)startDate end:(NSDate*)endDate
{
stepBegin=startDate;
stepEnd=endDate;
if ([HKHealthStore isHealthDataAvailable])
{
    HKUnit *unit = [HKUnit countUnit];

    HKQuantityType *stepCountType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];


    [self fetchMostRecentDataOfQuantityType:stepCountType withCompletion:^(HKQuantity *mostRecentQuantity, NSError *error) {
        if (!mostRecentQuantity)
        {
            //Either an error

        }
        else
        {
            double temCout=[mostRecentQuantity doubleValueForUnit:unit];
            coutStep=temCout;

        }
    }];

}
}

+ (void)fetchMostRecentDataOfQuantityType:(HKQuantityType *)quantityType withCompletion:(void (^)(HKQuantity *mostRecentQuantity, NSError *error))completion {
NSSortDescriptor *timeSortDescriptor = [[NSSortDescriptor alloc] initWithKey:HKSampleSortIdentifierEndDate ascending:NO];
//=======
NSDate *startDate, *endDate; // Whatever you need in your case
startDate=stepBegin;
endDate=stepEnd;
// Your interval: sum by hour
NSDateComponents *intervalComponents = [[NSDateComponents alloc] init];
intervalComponents.hour = 1;

// Example predicate
NSPredicate *predicate = [HKQuery predicateForSamplesWithStartDate:startDate endDate:endDate options:HKQueryOptionStrictStartDate];

// Since we are interested in retrieving the user's latest sample, we sort the samples in descending order, and set the limit to 1. We are not filtering the data, and so the predicate is set to nil.
HKSampleQuery *query = [[HKSampleQuery alloc] initWithSampleType:quantityType predicate:predicate limit:100 sortDescriptors:@[timeSortDescriptor] resultsHandler:^(HKSampleQuery *query, NSArray *results, NSError *error) {
    if (!results) {
        if (completion) {
            completion(nil, error);
        }
        return;
    }
    if (completion) {
        // If quantity isn't in the database, return nil in the completion block.
        HKQuantitySample *quantitySample = results.firstObject;
        HKQuantity *quantity = quantitySample.quantity;

        completion(quantity, error);
    }
}];

[healthStore executeQuery:query];
}

跳这个帮助!

答案 1 :(得分:1)

if (NSClassFromString(@"HKHealthStore") && [HKHealthStore isHealthDataAvailable])
{
    // Add your HealthKit code here
    HKHealthStore *healthStore = [[HKHealthStore alloc] init];

    // Share body mass, height and body mass index etc....
    NSSet *shareObjectTypes = [NSSet setWithObjects:
                               [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMass],
                               [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeight],
                               [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDistanceWalkingRunning],
                               [HKSampleType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount],
                               nil];

    // Read date of birth, biological sex and step count etc
    NSSet *readObjectTypes  = [NSSet setWithObjects:
                               [HKObjectType characteristicTypeForIdentifier:HKCharacteristicTypeIdentifierDateOfBirth],
                               [HKObjectType characteristicTypeForIdentifier:HKCharacteristicTypeIdentifierBiologicalSex],
                               [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDistanceWalkingRunning],
                               [HKSampleType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount],
                               nil];

    HKQuantityType *type = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];

    // Request access
    [healthStore requestAuthorizationToShareTypes:shareObjectTypes
                                        readTypes:readObjectTypes
                                       completion:^(BOOL success, NSError *error) {

                                           if(success == YES)
                                           {
                                               //[healthStore ];
                                               //NSPredicate *predicate = [HKQuery predicateForSamplesWithStartDate:startDate endDate:endDate options:HKQueryOptionStrictStartDate];

                                              // NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:HKSampleSortIdentifierStartDate ascending:YES];
                                               NSSortDescriptor *timeSortDescription = [[NSSortDescriptor alloc] initWithKey:HKSampleSortIdentifierEndDate ascending:NO];
                                               HKSampleQuery    *query = [[HKSampleQuery alloc] initWithSampleType:type
                                                                                                         predicate:nil
                                                                                                             limit:HKObjectQueryNoLimit
                                                                                                   sortDescriptors:@[timeSortDescription]
                                                                                                    resultsHandler:^(HKSampleQuery *query, NSArray *result, NSError *error){

                                                                                                        NSLog(@"RESULT  : =>  %@",result);
                                                                                                        if(!error && result)
                                                                                                        { long totalSteps=0;
                                                                                                            for(HKQuantitySample *quantitySample in result)
                                                                                                            {
                                                                                                                // your code here


                                                                                                            HKQuantity  *quantity=quantitySample.quantity;
                                                                                                                 //HKQuantity *quantity = quantitySample.quantity;
                                                                                                                NSString *string=[NSString stringWithFormat:@"%@",quantity];
                                                                                                                NSString *newString1 = [string stringByReplacingOccurrencesOfString:@" count" withString:@""];

                                                                                                                NSInteger count=[newString1 integerValue];
                                                                                                                totalSteps=totalSteps+count;
                                                                                                            }
                                                                                                            //using total steps
                                                                                                        }
                                                                                                    }];
                                               [healthStore executeQuery:query];
                                           }
                                           else
                                           {
                                               // Determine if it was an error or if the
                                               // user just canceld the authorization request
                                               //Fit_AAPLprofileviewcontroller_m.html
                                           }

                                       }];
}

答案 2 :(得分:0)

您可以使用HKSampleQuery对步骤(以及HealthKit中存储的任何其他样本)执行简单查询。如果您希望HealthKit为您汇总样本,可以使用HKStatisticsQueryHKStatisticsCollectionQuery代替。在查询用户的HealthKit数据之前,您需要获得-[HKHealthStore requestAuthorizationToShareTypes:readTypes:completion: ]访问权限的许可。

有关编写与HealthKit集成的应用程序的一般性介绍,我建议您观看WWDC talk