Ios 从Apple的健康应用程序访问数据步数

Ios 从Apple的健康应用程序访问数据步数,ios,healthkit,hksamplequery,Ios,Healthkit,Hksamplequery,我想在我的应用程序中显示我的步数标签 数据步数将取自苹果的健康应用程序,但我不知道是否可能 如何在标签中打印步长计数的值 这是我的密码 谢谢 #import "ViewController.h" @import HealthKit; @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; if(NSClassFromStrin

我想在我的应用程序中显示我的步数标签

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

如何在标签中打印步长计数的值

这是我的密码

谢谢

#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

是的,如果用户授予您的应用程序读取步骤计数的权限,您可以从HealthKit读取步骤计数

请参阅HealthKit文档:


我在文档中找不到答案