ios - 如何将我们在健康应用程序中输入的高度和体重数据读取到我们的自定义应用程序中
<p><p>如何将我们在健康应用中输入的高度和体重数据值读取到我们的自定义应用中?</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p><strong>1.请求 HealthKit 授权:</strong></p>
<pre><code>- (void)checkHealthStoreAuthorization
{
if () {
NSSet *readDataTypes = ;
if (!self.healthStore) {
self.healthStore = [ init];
}
static NSInteger i = 0;
for (HKObjectType * quantityType in readDataTypes) {
if ( == HKAuthorizationStatusNotDetermined) {
[self.healthStore requestAuthorizationToShareTypes:nil readTypes:readDataTypes completion:^(BOOL success, NSError *error) {
i++;
if (!success) {
if (error) {
NSLog(@"requestAuthorizationToShareTypes error: %@", error);
}
return;
} else {
if (i == ) {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
;
;
});
}
}
}];
} else {
if (]) {
;
} else if (]) {
;
}
}
}
}
}
- (NSSet *)dataTypesToRead
{
HKQuantityType *height = ;
HKQuantityType *weight = ;
return ;
}
</code></pre>
<p><强>2。获取高度和体重:</strong></p>
<pre><code>- (void)getHeight
{
NSDateComponents *interval = [ init];
interval.month = 1;
HKQuantityType *quantityType = ;
HKStatisticsCollectionQuery *query =
[ initWithQuantityType:quantityType
quantitySamplePredicate:nil
options:HKStatisticsOptionDiscreteAverage
anchorDate:
intervalComponents:interval];
query.initialResultsHandler = ^(HKStatisticsCollectionQuery *query, HKStatisticsCollection *results, NSError *error) {
if (error) {
NSLog(@"An error occurred while retrieving body mass: %@", error);
}
toDate:
withBlock:^(HKStatistics *result, BOOL *stop) {
HKQuantity *quantity = result.averageQuantity;
const int month = 30 * 24 * 60 * 60;
static BOOL isLastMonthResult;
static double lastHeight;
if ([ timeIntervalSinceDate:result.startDate] < month) {
isLastMonthResult = YES;
if (!quantity && lastHeight > 0.0) {
NSLog(@"Height: %lf", lastHeight);
}
}
if (quantity) {
lastHeight = ] * 100;
if (isLastMonthResult) {
NSLog(@"Height: %lf", lastHeight);
}
}
}];
};
;
}
- (void)getWeight
{
NSDateComponents *interval = [ init];
interval.month = 1;
HKQuantityType *quantityType = ;
HKStatisticsCollectionQuery *query =
[ initWithQuantityType:quantityType
quantitySamplePredicate:nil
options:HKStatisticsOptionDiscreteAverage
anchorDate:
intervalComponents:interval];
query.initialResultsHandler = ^(HKStatisticsCollectionQuery *query, HKStatisticsCollection *results, NSError *error) {
if (error) {
NSLog(@"An error occurred while retrieving body mass: %@", error);
}
toDate:
withBlock:^(HKStatistics *result, BOOL *stop) {
HKQuantity *quantity = result.averageQuantity;
const int month = 30 * 24 * 60 * 60;
static BOOL isLastMonthResult;
static double lastWeight;
if ([ timeIntervalSinceDate:result.startDate] < month) {
isLastMonthResult = YES;
if (!quantity && lastWeight > 0.0) {
NSLog(@"Weight: %lf", lastWeight);
}
}
if (quantity) {
lastWeight = ] / 1000;
if (isLastMonthResult) {
NSLog(@"Weight: %lf", lastWeight);
}
}
}];
};
;
}
- (NSDate *)startDate
{
NSCalendar *calendar = ;
NSDateComponents *components = ];
components.hour = 0;
components.year -= 1;
NSDate *startDate = ;
return startDate;
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - 如何将我们在健康应用程序中输入的高度和体重数据读取到我们的自定义应用程序中,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/29389540/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/29389540/
</a>
</p>
页:
[1]