-
-
Save anka/9aa9102664e424ff6a49 to your computer and use it in GitHub Desktop.
iOS query the step count
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Set your start and end date for your query of interest | |
NSDate *startDate, *endDate; | |
// Use the sample type for step count | |
HKSampleType *sampleType = [HKSampleType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount]; | |
// Create a predicate to set start/end date bounds of the query | |
NSPredicate *predicate = [HKQuery predicateForSamplesWithStartDate:startDate endDate:endDate options:HKQueryOptionStrictStartDate]; | |
// Create a sort descriptor for sorting by start date | |
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:HKSampleSortIdentifierStartDate ascending:YES]; | |
HKSampleQuery *sampleQuery = [[HKSampleQuery alloc] initWithSampleType:sampleType | |
predicate:predicate | |
limit:HKObjectQueryNoLimit | |
sortDescriptors:@[sortDescriptor] | |
resultsHandler:^(HKSampleQuery *query, NSArray *results, NSError *error) { | |
if(!error && results) | |
{ | |
for(HKQuantitySample *samples in results) | |
{ | |
// your code here | |
} | |
} | |
}]; | |
// Execute the query | |
[healthStore executeQuery:sampleQuery]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment