如何下载Healthkit步距和距离数据? [英] How do I download Healthkit step and distance data?

查看:109
本文介绍了如何下载Healthkit步距和距离数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在iPhone 5S(及更高版本)中下载运动处理器收集的步距和距离数据,并在Apple的HealthKit中进行分析.

I would like to download the step and distance data collected by the motion processor in the IPhone 5S (and later), and available in Apple's HealthKit, for analysis.

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

What's the easiest/best way to do this?

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

And clarifying (after new answers): is there any way to do it without writing a new iOS app? Are there any existing apps that provide the data, and/or any iCloud API that provides access.

推荐答案

我不确定它可以为您提供帮助,但这就是我的步骤

I'm not sure it can help you but this is how I get steps

+ (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];
}

希望此帮助!

这篇关于如何下载Healthkit步距和距离数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆