gpt4 book ai didi

iOS Healthkit 阅读锻炼

转载 作者:行者123 更新时间:2023-11-29 12:13:29 27 4
gpt4 key购买 nike

大家好,有人知道如何从 HealthKit 获取锻炼数据吗?我在本教程中看到了http://www.raywenderlich.com/89733/healthkit-tutorial-with-swift-workouts它是在迅速。我已经根据该教程在 Objective C 中进行了尝试,但结果为零。有关于保存锻炼的问题,但我想阅读锻炼数据并显示。

HKWorkoutType *workouttype = [HKWorkoutType workoutType];
HKWorkout *workout;
NSDate *startDate, *endDate;

NSDate *date1 = [NSDate date];
int daysTominus = -2;
startDate = [date1 dateByAddingTimeInterval:60*60*24*daysTominus];
int daysToAdd = 1;
NSDate *newDate1 = [date1 dateByAddingTimeInterval:60*60*24*daysToAdd];
endDate = newDate1;

workout = [HKWorkout workoutWithActivityType:HKWorkoutActivityTypeSwimming startDate:startDate endDate:endDate];

NSPredicate *predicate = [HKQuery predicateForObjectsFromWorkout:workout];

NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:HKSampleSortIdentifierStartDate ascending:YES];

HKSampleQuery *sampleQuery = [[HKSampleQuery alloc] initWithSampleType:workouttype
predicate:predicate
limit:HKObjectQueryNoLimit
sortDescriptors:@[sortDescriptor]
resultsHandler:^(HKSampleQuery *query, NSArray *results, NSError *error)
{
if(!error && results){
for(HKQuantitySample *samples in results)
{
// your code here
NSLog(@"%@",samples);
}
}
}];

// Execute the query
[healthStore executeQuery:sampleQuery];

最佳答案

问题似乎是你的谓词。

这段代码对我有用,我使用运行是因为我没有游泳数据,但你可以将其改回游泳:

    -(void)retrieveWorkouts{
// 1. Predicate to read only running workouts
NSPredicate *predicate = [HKQuery predicateForWorkoutsWithWorkoutActivityType:HKWorkoutActivityTypeWalking];

// 2. Order the workouts by date
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]initWithKey:HKSampleSortIdentifierStartDate ascending:false];

// 3. Create the query
HKSampleQuery *sampleQuery = [[HKSampleQuery alloc] initWithSampleType:[HKWorkoutType workoutType]
predicate:predicate
limit:HKObjectQueryNoLimit
sortDescriptors:@[sortDescriptor]
resultsHandler:^(HKSampleQuery *query, NSArray *results, NSError *error)
{

if(!error && results){
NSLog(@"Retrieved the following workouts");
for(HKQuantitySample *samples in results)
{
// your code here
HKWorkout *workout = (HKWorkout *)samples;
NSLog(@"%f",workout);
}
}else{
NSLog(@"Error retrieving workouts %@",error);
}
}];

// Execute the query
[healthStore executeQuery:sampleQuery];
}

关于iOS Healthkit 阅读锻炼,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32608155/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com