简体   繁体   English

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

[英]Tell if a Health Kit sample came from an Apple Watch?

Health App displays a Watch icon when the source was an Apple Watch. 当源是Apple Watch时,Health App会显示Watch图标。

I'd simply like to get the same information that the Health App is using to determine the type of source. 我只想获得Health App用来确定源类型的相同信息。 HKSource doesn't seem to provide that. HKSource似乎没有提供。

since iOS 9, samples of type HKSample have the property device of type HKDevice . 自的iOS 9,类型的样品HKSample具有属性device类型的HKDevice

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

HKDevice tells you everything about the source device of the sample. HKDevice告诉您有关样本源设备的所有信息。

HKDevice.model describes the hardware type. HKDevice.model描述了硬件类型。 At the time of writing, Apple does not document what values Apple uses in HKDevice.model . 在撰写本文时,Apple没有记录Apple在HKDevice.model使用的HKDevice.model In my experiments, I found values "iPhone" and "Watch". 在我的实验中,我发现了“iPhone”和“Watch”的价值观。

I had a similar problem in my app, where we needed to pull steps data only specific to Apple iPhone and Apple Watch only. 我在我的应用程序中遇到了类似的问题,我们只需要提取仅适用于Apple iPhone和Apple Watch的步骤数据。 I searched a lot, but could not find any answer to it for iOS 8.0, and landed on your question. 我搜索了很多,但在iOS 8.0中找不到任何答案,并找到了你的问题。

I have figured out a way to distinguish between the watch and the phone using the following process (may not be the best solution, but works in my situation): 我已经找到了一种方法来区分手表和手机使用以下过程(可能不是最好的解决方案,但在我的情况下工作):

I noticed that all step data coming from the iPhone/Watch have the following bundleIdentifier format: 我注意到来自iPhone / Watch的所有步骤数据都具有以下bundleIdentifier格式:

com.apple.health.DeviceUUID com.apple.health.DeviceUUID

Note that manually entered data into the Health app has a bundle identifier of com.apple.Health (with a capital 'H'). 请注意,在Health应用程序中手动输入的数据的包标识符为com.apple.Health(大写为“H”)。

So, first thing, get the device name for the phone using: 所以,首先,使用以下方法获取手机的设备名称:

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

Next, fetch all the sources for which there is a prefix match of 'com.apple.health' in the bundleIdentifier. 接下来,获取bundleIdentifier中前缀匹配为'com.apple.health'的所有源。 This should give you the iPhone and the Apple watch as the valid sources and ignore the manual entries and all other apps. 这应该为您提供iPhone和Apple手表作为有效来源,并忽略手动条目和所有其他应用程序。

Next, check if the name of the device is the same in the source, and ignore that source (iPhone), the other source should be your Apple Watch. 接下来,检查源中的设备名称是否相同,并忽略该源(iPhone),另一个源应该是Apple Watch。

Here's a sample source query for fetching the sources : 以下是获取源的示例源查询:

- (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];
}

You can now create a predicate with this source for your data pull using the NSPredicate class: 现在,您可以使用NSPredicate类为此数据提取源创建一个谓词:

NSPredicate *sourcesPredicate = [HKQuery predicateForObjectsFromSource:source];

Note that my first thought was to match the UUID, but when I generate a UUID using the NSUUID class, it does not match with the one present in the bundle identifier in the pulled sources. 请注意,我的第一个想法是匹配UUID,但是当我使用NSUUID类生成UUID时,它与拉出源中的包标识符中存在的UUID不匹配。

Also, you can change the name of the phone to whatever you want, it will automatically update in the Health app as well. 此外,您可以将手机名称更改为您想要的任何名称,它也会在Health应用程序中自动更新。

As I said, not the best solution but works for me, and it's the only way I could find to do this. 正如我所说,不是最好的解决方案,但对我有用,这是我能找到的唯一方法。 Please let me know if you were able to find a better solution. 如果您能找到更好的解决方案,请告诉我。 Thanks. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM