Ios 将健康套件数据访问到Apple Watch OS 2,不包括训练数据

Ios 将健康套件数据访问到Apple Watch OS 2,不包括训练数据,ios,iphone,watchos-2,healthkit,hksamplequery,Ios,Iphone,Watchos 2,Healthkit,Hksamplequery,我能够通过训练课程访问训练数据,但无法对其他人进行同样的操作,例如访问身高、体重、膳食水、体温、血压等 此外,我可以访问心率,但无法访问体温。它们都是相同的生命体征标识符 手表是否只能访问WWDC 2015视频中提到的训练数据 示例代码: -(void)bodyTempForLabel :(WKInterfaceLabel *)bodyTempLabel { HKSampleType *bodyTemp = [HKQuantityType quantityTypeForIdentif

我能够通过训练课程访问训练数据,但无法对其他人进行同样的操作,例如访问身高、体重、膳食水、体温、血压等

此外,我可以访问心率,但无法访问体温。它们都是相同的生命体征标识符

手表是否只能访问WWDC 2015视频中提到的训练数据

示例代码:

-(void)bodyTempForLabel :(WKInterfaceLabel *)bodyTempLabel {

    HKSampleType *bodyTemp = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyTemperature];

    [self readMostRecentSampleType:bodyTemp withCompletion:^(HKQuantitySample *quantitySample, NSError *error) {

        if(error) {

            NSLog(@"Error Reading Weight From health Kit");
        }

        self.bodyTemp = quantitySample;

        double bodyTempinDegree = [[self.bodyTemp quantity] doubleValueForUnit:[HKUnit unitFromString:[NSString stringWithFormat:@"%@C", @"\u00B0"]]];

        dispatch_async(dispatch_get_main_queue(), ^{

            [bodyTempLabel setText:[NSString stringWithFormat:@"%f",bodyTempinDegree]];
        });

    }];
}

-(void)readMostRecentSampleType : (HKSampleType *)sampleType withCompletion:(void(^)(HKQuantitySample *quantitySample,NSError *error))recentSample {

    NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:HKSampleSortIdentifierEndDate ascending:NO];

    HKQuery *sampleQuery = [[HKSampleQuery alloc] initWithSampleType:sampleType predicate:nil limit:HKObjectQueryNoLimit sortDescriptors:@[sortDescriptor] resultsHandler:^(HKSampleQuery * _Nonnull query, NSArray<__kindof HKSample *> * _Nullable results, NSError * _Nullable error) {

        if(!error) {
            // No results retuned array empty
           HKQuantitySample *mostRecentSample = results.firstObject;

            recentSample(mostRecentSample,error);
        }

    }];

    [_healthStore executeQuery:sampleQuery];

}
-(void)bodyTempForLabel:(WKInterfaceLabel*)bodyTempLabel{
HKSampleType*bodyTemp=[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyTemperature];
[自读MOStrecentSampleType:bodyTemp with Completion:^(HKQuantitySample*quantitySample,N错误*错误){
如果(错误){
NSLog(@“从健康套件读取重量时出错”);
}
self.bodyTemp=quantitySample;
double bodyTempinDegree=[[self.bodyTemp quantity]double ValueForUnit:[HKUnitUnitFromString:[NSString stringWithFormat:@“%@C”,“@”\u00B0“]];
dispatch\u async(dispatch\u get\u main\u queue()^{
[bodyTempLabel setText:[NSString stringWithFormat:@“%f”,bodyTempinDegree];
});
}];
}
-(void)readMostRecentSampleType:(HKSampleType*)带补全的sampleType:(void(^)(HKQuantitySample*quantitySample,NSError*error))recentSample{
NSSortDescriptor*sortDescriptor=[NSSortDescriptor sortDescriptor With Key:HKSampleSortIdentifierEndDate升序:否];
HKQuery*sampleQuery=[[HKSampleQuery alloc]initWithSampleType:sampleType谓词:零限制:HKObjectQueryOnlimit sortDescriptors:@[sortDescriptor]resultsHandler:^(HKSampleQuery*\非空查询,NSArray*\可空结果,NSError*\可空错误){
如果(!错误){
//没有结果返回空数组
HKQuantitySample*mostRecentSample=results.firstObject;
最近样本(最新样本,错误);
}
}];
[\u healthStore executeQuery:sampleQuery];
}

任何帮助都将不胜感激。谢谢

apple watch可以访问所有类型的健康套件(尽管只有部分数据)。您的应用程序是否已请求所有这些类型的权限?在设置健康商店时,需要明确要求您读取或写入的每种类型。例如,要读取能量消耗、距离和心率,您需要包括:

let typesToRead = Set([
    HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierActiveEnergyBurned)!,
    HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierDistanceWalkingRunning)!,
    HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeartRate)!
])

self.healthStore.requestAuthorizationToShareTypes(typesToShare, readTypes: typesToRead) { success, error in
    // ...
}

看来您需要使用真正的设备进行调试。在运行模拟器时,我无法从HK获得任何价值,但它在Apple Watch中运行良好。(使用XCode 7 Beta 5)。

是的,我已申请了所有这些类型的读取权限,并且出现了“健康套件权限”窗口提示。但是,您能否从apple watch访问身高和体重数据?是的。我从设备读取(和写入)所有健康数据,但不从模拟器读取(和写入)。