从Apple的Health应用程序访问数据步数

时间:2015-06-09 14:39:29

标签: ios health-kit hksamplequery

我想在我的应用中显示我的步数

的标签

数据步数将来自苹果的健康应用,但我不知道是否可能

我如何在标签中打印步数的值?

这是我的代码

谢谢

#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 个答案:

答案 0 :(得分:1)

是的,如果用户授予您的应用权限,您可以从HealthKit中读取步数。

请参阅HealthKit文档:

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