从 Apple 的 Health 应用程序访问数据步数
Posted
技术标签:
【中文标题】从 Apple 的 Health 应用程序访问数据步数【英文标题】:Access Data step count from Health app of Apple 【发布时间】:2015-06-09 14:39:28 【问题描述】:我想在我的应用中显示我的步数标签
数据步数将从苹果的健康应用程序中获取,但我不知道是否可能
如何在标签中打印步数的值?
这是我的代码
谢谢
#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 文档:
https://developer.apple.com/library/ios/documentation/HealthKit/Reference/HealthKit_Framework/index.html#//apple_ref/doc/uid/TP40014707
【讨论】:
我在文档中找不到答案以上是关于从 Apple 的 Health 应用程序访问数据步数的主要内容,如果未能解决你的问题,请参考以下文章
忽略来自 Apple Health 应用程序的手动输入作为数据源
忽略来自 Apple Health 应用程序的手动输入作为数据源
React Native 开发中 Apple Health Kit 的虚假步骤数据
如果您使用的是 Apple Watch,是不是需要先打开 Health App 同步数据,然后再打开自己的 healthkit 应用程序获取最新数据?