Количество шагов доступа к данным из приложения "Здоровье" Apple
Я хотел бы показать в моем приложении метку моего количества шагов
Количество шагов данных будет взято из приложения для здоровья Apple, но я не знаю, будет ли это возможно
Как я могу напечатать значение моего количества шагов в метке?
Это мой код
Спасибо
#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 ответ
Да, вы можете прочитать количество шагов из HealthKit, если пользователь предоставит разрешение вашему приложению.
Смотрите документацию HealthKit: