Access Data step count from Health app of Apple

1.3k views Asked by At

I would like to show in my app a label of my step count

The data step count will be take from the health app of apple but i don't know if will be possible

how i can print the value of my step count in a label ?

This is my code

Thanks

#import "ViewController.h"
@import HealthKit;


@interface ViewController ()

@end

@implementation ViewController


- (void)viewDidLoad {
[super viewDidLoad];
if(NSClassFromString(@"HKHealthStore") && [HKHealthStore isHealthDataAvailable])
{


    HKHealthStore *healthStore = [[HKHealthStore alloc] init];
    NSSet *shareObjectTypes = [NSSet setWithObjects:
                               [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMass],
                               [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeight],
                               nil];
    NSSet *readObjectTypes  = [NSSet setWithObjects:
                               [HKObjectType characteristicTypeForIdentifier:HKCharacteristicTypeIdentifierDateOfBirth],
                               [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount],
                               nil];

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

                                           if(success == YES)
                                           {
                                               // 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];
                                               _stepLabel.text = [NSString stringWithFormat:@"%@",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) {
                                                                                                           NSLog(@"%@ ", results);

                                                                                                           if(!error && results)
                                                                                                           {
                                                                                                               for(HKQuantitySample *samples in results)
                                                                                                               {
                                                                                                                   // your code here
                                                                                                               }
                                                                                                           }

                                                                                                       }];

                                               // Execute the query
                                               [healthStore executeQuery:sampleQuery];                                               }
                                           else
                                           {
                                               // Determine if it was an error or if the
                                               // user just canceld the authorization request
                                           }

                                       }];



     }}

 - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end
1

There are 1 answers

1
joshstaiger On

Yes, you can read the step count from HealthKit if the user grants permission for your app to do so.

See the HealthKit documentation:

https://developer.apple.com/library/ios/documentation/HealthKit/Reference/HealthKit_Framework/index.html#//apple_ref/doc/uid/TP40014707