告诉健康套件样品是否来自Apple Watch?

时间:2015-06-29 07:37:23

标签: ios apple-watch health-kit

当源是Apple Watch时,Health App会显示Watch图标。

我只想获得Health App用于确定源类型的相同信息。 HKSource似乎没有提供。

2 个答案:

答案 0 :(得分:6)

自iOS 9以来,HKSample类型的样本的device类型属性为HKDevice

https://developer.apple.com/library/prerelease/ios/documentation/HealthKit/Reference/HKDevice_ClassReference/index.html

HKDevice会告诉您样本源设备的所有信息。

HKDevice.model描述了硬件类型。在撰写本文时,Apple没有记录Apple在HKDevice.model中使用的值。在我的实验中,我发现了值“iPhone”和“观察”。

答案 1 :(得分:4)

我的应用程序中存在类似的问题,我们只需要提取仅适用于Apple iPhone和Apple Watch的步骤数据。我搜索了很多,但在iOS 8.0中找不到任何答案,并找到了你的问题。

我已经找到了一种方法来区分手表和手机使用以下过程(可能不是最好的解决方案,但在我的情况下有效):

我注意到来自iPhone / Watch的所有步骤数据都具有以下bundleIdentifier格式:

<强> com.apple.health.DeviceUUID

请注意,在Health应用程序中手动输入的数据的包标识符为com.apple.Health(使用大写&#39; H&#39;)。

首先,请使用以下方法获取手机的设备名称:

setTimeout(f,0)

接下来,获取所有与&#39; com.apple.health&#39;前缀匹配的来源。在bundleIdentifier中。这应该为您提供iPhone和Apple手表作为有效来源,并忽略手动条目和所有其他应用程序。

接下来,检查设备中的设备名称是否相同,并忽略该源(iPhone),其他来源应该是Apple Watch。

以下是获取来源的示例源查询:

NSString *deviceName = [[UIDevice currentDevice] name];

现在,您可以使用NSPredicate类为您的数据提取创建一个具有此源的谓词:

- (void)fetchSources 
{
    NSString *deviceName = [[UIDevice currentDevice] name];
    NSMutableArray *dataSources = [[NSMutableArray alloc] init];
    HKQuantityType *stepsCount = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];
    HKSourceQuery *sourceQuery = [[HKSourceQuery alloc] initWithSampleType:stepsCount
                                                           samplePredicate:nil
                                                         completionHandler:^(HKSourceQuery *query, NSSet *sources, NSError *error)
                                                         {
                                                             for (HKSource *source in sources)
                                                             {
                                                                 //Ignore the iPhone as a source as the name of the device will watch with the source.
                                                                 //The other device will be an Apple Watch   
                                                                 if ([source.bundleIdentifier hasPrefix:sourceIdentifier] && ![source.name isEqualToString:deviceName])
                                                                 {
                                                                     [dataSources addObject:source];
                                                                 }
                                                             }
                                                         }];
    [self.healthStore executeQuery:sourceQuery];
}

请注意,我的第一个想法是匹配UUID,但是当我使用NSUUID类生成UUID时,它与拉出源中的包标识符中存在的UUID不匹配。

此外,您可以将手机名称更改为您想要的任何名称,它也会在Health应用程序中自动更新。

正如我所说,不是最好的解决方案,但对我有用,而且这是我能找到的唯一方法。如果您能找到更好的解决方案,请告诉我。感谢。