1

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

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

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

4

3 回答 3

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

跳这个帮助!

于 2014-11-11T01:56:09.263 回答
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
                                           }

                                       }];
}
于 2014-12-17T10:33:05.957 回答
0

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

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

于 2014-11-14T16:19:35.873 回答