【问题标题】:Accessing Health Kit data into Apple Watch OS 2 excluding Workout Data将 Health Kit 数据访问 Apple Watch OS 2,不包括锻炼数据
【发布时间】:2015-08-13 13:44:15
【问题描述】:

我可以使用锻炼会话访问锻炼数据,但无法对其他数据进行同样的操作,例如访问身高、体重、饮食水、体温、血压等。

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

手表是否只能访问 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];

}

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

【问题讨论】:

    标签: ios iphone watchos-2 healthkit hksamplequery


    【解决方案1】:

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

    【讨论】:

    • 但是你可以从 Apple Watch 获取身高和体重数据吗?
    • 是的。我从设备读取(和写入)所有健康数据,但不是从模拟器中读取。
    【解决方案2】:

    Apple Watch 可以访问所有健康工具包类型(尽管只是数据的一个子集)。您的应用是否已请求所有这些类型的许可?在设置健康存储时,需要明确询问您要读取或写入的每种类型。例如,要读取消耗的能量、距离和心率,您需要包括:

    let typesToRead = Set([
        HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierActiveEnergyBurned)!,
        HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierDistanceWalkingRunning)!,
        HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeartRate)!
    ])
    
    self.healthStore.requestAuthorizationToShareTypes(typesToShare, readTypes: typesToRead) { success, error in
        // ...
    }
    

    【讨论】:

    • 是的,我已请求所有这些类型的读取权限,并且我也收到了健康工具包权限窗口的提示。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多